The tilde is expanded in the assignment to PATH
because it is a variable assignment and the tilde comes just after an unquoted colon (in your example).
A "tilde-prefix" consists of an unquoted <tilde>
character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash>
in the word, or all the characters in the word if there is no <slash>
. In an assignment (see XBD Variable Assignment), multiple tilde-prefixes can be used: at the beginning of the word (that is, following the <equals-sign>
of the assignment), following any unquoted <colon>
, or both.
(from the POSIX text on tilde expansion)
The bash
manual puts it like
Each variable assignment is checked for unquoted tilde-prefixes
immediately following a :
or the first =
. In these cases, tilde
expansion is also performed. Consequently, one may use filenames with
tildes in assignments to PATH
, MAILPATH
, and CDPATH
, and the shell
assigns the expanded value.
This means that assigning the unquoted string /program_files:~/home/t
to PATH
will expand the tilde within, and that $PATH
will be the string with the tilde expanded.
Placing a literal tilde in PATH
, for example by quoting the string, will cause pathname resolution of commands to fail for that directory (unless there is a directory in the current working directory with that literal name).
bash
when not in POSIX mode, will still expand these tildes in PATH
when looking for commands.
When the shell scans the line
mypath=/program_files:~/home/t
it is returned to the parser as a single token. It will be processed as a simple command.
A simple command, when recognised as an assignment, undergoes, among other things, tilde expansion. While doing the tilde expansion on the right hand side of the =
, the shell will expand the tilde in the string to the home directory of the current user, because it occurs just after a colon.
See also the POSIX text on simple commands.
mypath=/program_files:~/home/t
split into? – Tim Jun 07 '18 at 21:09=
and:
not metacharacters? What are they? – Tim Jun 07 '18 at 21:36=
and:
"words" in bash's definition? – Tim Jun 07 '18 at 21:40mypath=/program_files:~/home/t
is a single token. It is processed as a simple command. – Kusalananda Jun 07 '18 at 21:40<
can be redirection operator, or comparions operator between two numbers.&
,(
and)
also can have different meanings. So does a metacharactor with any of the meanings server as token separator? – Tim Jun 07 '18 at 22:04ps aux|less
andps aux | less
are equivalent:|
doesn’t lose its piping effect when it’s the only separator between words. – Stephen Kitt Jun 08 '18 at 07:44