I want a variable, let’s say PS, defined in my .profile file with the following value:
PS1='PWD: ${PWD}
$ '
So that when I enter $PS in my terminal, I get a two-lined prompt string, the first line showing my working directory. How can I define this in my .profile file?
Edit: the question that this is purportedly a duplicate of is not the same at all. My question is about assigning a multiline value to a variable in a shell script. Neither \n nor \ followed by Enter appear to work. Note that the assignment I wrote works perfectly well interactively. In fact, I have it saved it in my OneNote and often copy/paste it in my shell. But I would like it to be in my .profile file or another script so I can skip this step, and writing it as is in a script does not work. Finally, if someone suggests a change where PS1 is changed automatically, that is OK, but the ideal would be if the whole thing above was assigned to a variable like PS so that when I typed $PS, PS1 would be set.
Edit 2: I found a solution. In .profile:
PS1="PWD: ${PWD}
\$ "
This changes PS1 only when I run
. .profile
Which is weird because I know .profile is being run in some way on startup (I try echoing variables that are defined in .profile before running it and they exist; I’m guessing there is some other file that is being run automatically after .profile that sets PS1 to the default one-liner), but I guess it’s what I want because I want some control over whether my change is effective. If I want my change to be effective I run . .profile, if not I don’t do anything. Sorry for the confusion, I was trying to experiment and update the description as fast as I could!
$(pwd)instead of${PWD}.profile. And it appears to work with both ksh and Bash at least on my Mac.cd.$PSin my terminal” angle doesn’t make any sense to me. (2) You say it “does not work”, but you have declined to explain in what way it fails. (3) And since your solution is the same as what you started with (except for the\$), I don’t know what you were ever asking. (4) As soon as you submit your first edit for review, people will start considering your question for reopening. You should not rush this step. Make the best argument you can for why the question should be reopened — it’s OK if you take a few days.