-2

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!

12
  • What you have should work. Although I'm guessing you want $(pwd) instead of ${PWD} Commented Feb 12, 2024 at 1:45
  • @jesse_b what I have works interactively, not in a script. Commented Feb 15, 2024 at 13:42
  • I can't see why that assignment above with a quoted hard newline shouldn't work in .profile. And it appears to work with both ksh and Bash at least on my Mac. Commented Feb 15, 2024 at 13:49
  • @ilkkachu Thanks for the poke. I would agree with all that you've said. The assignment should be working with no issues. The variable would be expanded at the time of the assignment, though, which is the issue that the duplicate is dealing with. I see nothing in the question indicating in what way the assignment does not work and when I closed it as a duplicate I presumed that the only issue with it was the fact that the working directory would never update when the user changed it with cd. Commented Feb 15, 2024 at 13:57
  • 1
    (1) I’m sorry, but the whole “when I enter $PS in 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. Commented Feb 15, 2024 at 15:14

1 Answer 1

-1

To define the PS variable in your .profile file with the specified value, you can simply add the following line:

export PS1="PWD: \${PWD}\n\$ "
3
  • 1
    Why do you export the variable? Do you know for a fact that the ksh shell interprets \n as a newline when it's part of the PS1 variable? Commented Feb 12, 2024 at 7:56
  • I did this and it does not interpret \n correctly. It says PWD: followed by my working directory and then n$ , all on one line. Commented Feb 14, 2024 at 21:53
  • 1
    this works in Bash, but it doesn't appear to work in ksh (at least not the ksh that came with my Mac) Commented Feb 15, 2024 at 13:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.