2

I am setting the following in my users .profile file to display the last 3 directories in PWD but it's still only showing the home directory. Not sure what is wrong with this. Can anyone help, please?

me='$(whoami)'
function PWD {
pwd | rev| awk -F / '{print $1,$2,$3}'|rev|sed 's/ /\//g'
}
export PS1="$(whoami)@$(hostname -s)$PWD$ "

It keeps showing the user's home directory.

user3164720
  • 73
  • 1
  • 7
  • 4
    `$PWD` != `$(PWD)` – bishop Jan 09 '18 at 20:14
  • @bishop That's a silly mistake on my part. I changed that but still only get the home directory. – user3164720 Jan 09 '18 at 20:25
  • 1
    BTW, `function PWD {` is ksh-ism (bash also accepts the syntax, but doesn't honor all the special behavior ksh applies to that style of declaration). You don't specify a specific shell in your question, but if you want general POSIX compatibility, you should use `PWD() {` with no `function` instead. – Charles Duffy Jan 09 '18 at 20:38
  • 2
    Also, choosing to `export PS1` rather than just assigning it is... an interesting choice. That means you're copying it to the environment to be read by subprocesses; since those subprocesses won't have the `PWD` function defined, it's arguably just plain wrong. – Charles Duffy Jan 09 '18 at 20:39
  • Anyhow, *if* I were for some reason going to build a really inefficient-to-render prompt that didn't use the available built-in backslash sequences (any command substitution like `$()` involves forking off a subshell, so using them is expensive and consequently best avoided), I'd use single quotes for the assignment: `PS1='$(whoami)@$(hostname -s)$PWD\$ '` – Charles Duffy Jan 09 '18 at 20:41
  • (then again, if we were going to care about performance, running a pipeline with four separate external commands in it every time we want to print a prompt is arguably a bigger impact than either `whoami` or `hostname` alone). – Charles Duffy Jan 09 '18 at 22:44
  • Thanks @CharlesDuffy. These are all good suggestions. But i am trying to use the .profile file to shorten my pwd to the last 3 directories which is sometimes 8-10 directories deep and looks ugly. At the same time, i am running a env.sh script to load a bunch of environment variables. `PREDIR() { pwd | rev| awk -F / '{print $1,$2,$3}'|rev|sed 's/ /\//g' } export PS1="$(whoami)@$(hostname -s):$(PREDIR)$ " . /opt/server/$(hostname -s)/bin/env.sh` I can't edit the env.sh file for proprietary reasons, but it changes the PS1 prompt if i don't use export PS1. What's the best way to do this? – user3164720 Jan 09 '18 at 23:44

2 Answers2

1

Per the Bash Prompt How To, emphasis mine:

You can use the output of regular Linux commands directly in the prompt as well....

[21:58:33][giles@nikola:~]$ PS1="[\$(date +%H%M)][\u@\h:\w]\$ "
[2159][giles@nikola:~]$ ls
bin   mail
[2200][giles@nikola:~]$

It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment. For this prompt, that would mean that it would display the same time no matter how long the prompt was used. The backslash protects the contents of $() from immediate shell interpretation, so date is called every time a prompt is generated.

bishop
  • 37,830
  • 11
  • 104
  • 139
  • 1
    Any reason to use double quotes for value at all? No need for (those) backslashes when the string is single-quoted. – Charles Duffy Jan 09 '18 at 20:39
  • None, in this case, @CharlesDuffy. A general case might be if colors are wanted, and those color escape values are variable held. Eg: `PS1="$red_on\$(date)$red_off"` – bishop Jan 09 '18 at 20:41
  • 1
    BTW, if only trying to support a newer bash, I'd use `PROMPT_COMMAND` to invoke `printf -v current_time %(%H%M)T -1`, and then refer to `$current_time` in `$PS1` -- that way we avoid the expense of a command substitution. – Charles Duffy Jan 09 '18 at 20:43
1

In this link - http://ezprompt.net/, you can customize your PS1 variable easily. Then run the command generated i.e "export PS1=..."

Himansu
  • 308
  • 4
  • 15