Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 08 Jan 2000 18:18:43 -0800
From:      R Joseph Wright <rjoseph@nwlink.com>
To:        keramida@ceid.upatras.gr, freebsd-questions@FreeBSD.org
Subject:   Re: Bash prompt (Was: window manager question)
Message-ID:  <3877F003.B0D0B3F8@nwlink.com>
References:  <200001052224.XAA37273@dorifer.heim3.tu-clausthal.de> <387435D1.7E20347D@nwlink.com> <20000106175704.A7246@hades.hell.gr> <387554AC.C953BD4D@nwlink.com> <20000108172255.B12562@hades.hell.gr>

next in thread | previous in thread | raw e-mail | index | archive | help
Giorgos Keramidas wrote:
> 
> [ taken out of freebsd-questions ]
> 
> Dear Joseph.
> 
> Shell variables are places where your shell keeps some values, for
> later use, for making your life easier, for altering it's behavior,
> etc. etc.
> 
> You can get the notion of shell variables from the following examples
> of interaction with the /usr/local/bin/bash on a command prompt:
> 
>         $ PATH="/bin:/usr/bin"
>         $ echo $PATH
>         /bin:/usr/bin

If I use the command line to tell the shell which PATH to use, how long
will it 'remember' PATH?  Just until I exit that shell? 
> 
> This sets a shell variable to one string value.  If you pass more
> variables to `echo' a space gets inserted between each value printed.
> See:
> 
>         $ echo $PATH $PATH
>         /bin:/usr/bin /bin:/usr/bin
> 
> But you might not want to have a space automagically inserted.  For
> instance, if you want to print the PATH twice, with a `:' character
> between each PATH printed, something like:
> 
>         $ echo $PATH : $PATH
>         /bin:/usr/bin : /bin:/usr/bin
> 
> The result is not quite right :-| What you can do is take advantage of
> a feature of the shell called `variable substitution' which takes place
> for all strings enclosed in double quotes.  This happens BEFORE `echo'
> will have a chance to `look' at the arguments it was passed, and it
> works transparently under the hood of your command line :)
> 
> With a few words, sequences like $NAME_OF_VARIABLE are substituted with
> the value of the respective variable in "..." strings.  See this:
> 
>         $ echo "Your path is: $PATH"
>         Your path is: /bin:/usr/bin
> 
> Note the output of the next command though:
> 
>         $ echo "===$PATH"
>         ===/bin:/usr/bin
> 
> Note that there is no additional space between === and /bin !
> 
> Shell variables can be substituted even in another funny form:
> 
>         $ echo "${PATH}5"
>         /bin:/usr/bin5
> 
> Note that if we used:
> 
>         $ echo "$PATH5"
> 
> the shell would have looked for a variable called `PATH5' and since no
> such variable can be found, nothing would be printed in the place of
> the shell variable PATH5.  The brackets ensure that the name of the
> variable stops at {PATH} and all works still fine.
> 
> Now, what if I want to print a dollar sign itself ? ? ?
> The shell can inhibit shell expansion at the sight of a dollar sign, if
> that sign is preceded by a backslash (\) character.  Look at this:
> 
>         $ echo "This is your \$PATH"
>         This is your $PATH
> 
> See that the expansion of PATH was inhibited because the dollar sign
> was preceded by backslash.  This resulted in a literal $ being printed.
> If we want ALL the dollar signs to be printed literally, instead of
> `escaping' each one with a backslash, we might use *simple* quotes in
> the place of the double quotes as in:
> 
>         $ echo 'Shell variables $PATH and $PS1 look nice in print.'
>         Shell variables $PATH and $PS1 look nice in print.
> 
> Now, I think that the difference of the commands below seems pretty
> clear to you.  If it doesn't it's probably my fault, and I will be glad
> to clarify what makes it unclear for you :)
> 
>         $ echo "PS1 is set to:[$PS1]"
>         PS1 is set to:[$ ]
>         $ PS1="$USER> "
>         charon> PS1="\$USER> "
>         $USER> PS1='$ '
>         $ PS1="\$ "                     # the same as above
>         $ echo $PS1
>         $
>         $ PS1='\$ '                     # no expansion, backslach included
>         $ echo $PS1
>         \$                              # See that backslash literal?
>         $ su -m
>         # echo $PS1
>         \$
>         # PS1="\$ "
>         $ echo $PS1
>         $                               # no literal backslash in there!
>         $ PS1="\\\$"                    # but watch this !!!
>         # echo $PS1
>         \$
> 
> Now if you can explain to yourself this last command with the three
> backslashes in row, you are truly an amazing folk, and nothing will
> make you gasp in awe when it comes to shell variables anymore :))))

Nope, you got me there.  I did some experimenting and discovered that up
to eight backslashes will show up in the prompt only as one.  Nine
through sixteen shows up as two, and so on counting by eights.  I didn't
figure out why doing 'echo $PS1' only shows half of the original
backslashes entered, for example:

$ PS1="\\\\\\\\ "
\$ echo $PS1
\\\\$
\$

> Ciao,
> and have fun reading all this crap I wrote tonight :)

Thank you, it was fun and very informative.
 
> --
> Giorgos Keramidas, < keramida @ ceid . upatras . gr >
> "What we have to learn to do, we learn by doing." [Aristotle]

-- 
Best Regards, Joseph

	You will do foolish things,	
     but do them with enthusiasm.  Colette.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3877F003.B0D0B3F8>