How to Escape Colon (:) in $Path on Unix

How to escape colon (:) in $PATH on UNIX?

This is impossible according to the POSIX standard. This is not a function of a specific shell, PATH handling is done within the execvp function in the C library. There is no provision for any kind of quoting.

This is the reason why including certain characters (anything not in the "portable filename character set" - colon is specifically called out as an example.) is strongly recommended against.

From SUSv7:

Since <colon> is a separator in this context, directory names that might be used in PATH should not include a <colon> character.

See also source of GLIBC execvp. We can see it uses the strchrnul and memcpy functions for processing the PATH components, with absolutely no provision for skipping over or unescaping any kind of escape character.

If there is a colon (:) in the directory name, how could I add it to $PATH?

As far as I know, you can't. The obvious way to escape a : character in $PATH would be to use a backslash, but a quick experiment with Bash on Linux indicates that it doesn't work. OSX might behave differently, but I suspect you'd have the same problem.

Your best bet is to rename the directory. If it really needs to have that name, you can create a symbolic link and add that to your $PATH:

 $ cd /path/to/add
$ ln -s a:b a_b
$ PATH="$PATH:/path/to/add/a_b/bin"

What does the colon do in PATH

: is the separator. The PATH variable is itself a list of folders that are "walked" through when you run a command.

In this case, the folders on your PATH are:

  • /Users/chengluli/anaconda/bin
  • /Users/chengluli/.rbenv/shims
  • /

Parsing and Printing $PATH Using Unix

Yet another way:

echo $PATH | tr : '\n'

or:

tr : '\n' <Path.txt

ZSH - How can I join an array using the colon : character/escape colon character?

Re zsh: error in flags: This one is easy. You can use any other delimiter in your parameter expansion flags:

$ arr=(/usr/local/bin /usr/bin /usr/sbin /bin)
$ print -R ${(j|:|)arr}
/usr/local/bin:/usr/bin:/usr/sbin:/bin

Your bigger problem is you shouldn't need to do this in the first place. Zsh specifically has a lowercase path variable which is the array counterpart of the scalar PATH. Setting one automatically updates the other. See http://zsh.sourceforge.net/Doc/Release/Parameters.html#index-path. (Similarly there are cdpath, fpath, mailpath, manpath, etc.) Here's an example:

$ path=(/usr/bin /bin)
$ print -R $PATH
/usr/bin:/bin
$ path=(/usr/local/bin /usr/bin /usr/sbin /bin)
$ print -R $PATH
/usr/local/bin:/usr/bin:/usr/sbin:/bin
$ PATH=/usr/bin:/bin
$ print -R $path
/usr/bin /bin

You get the idea.

As an aside, I also recommend

typeset -gU path

to remove duplicates, because it's not uncommon to insert duplicate entries into path (when you insert paths the right way: prepend to the existing array).

How do I escape an ! so terminal sees a closing dquote for echo Hello World!

About question 1: (How do you escape an !?)

You can use "'!'"

Example:

echo "Hello World"'!'""'!'" I'm there."

Result:

Hello World!! I'm there.

About question 2: (Why are double quotes different than single quotes here?)

Because ! is the default "history expansion" character and inside double quotes "history expansion" is performed.

Bash manual:

3.1.2.2 Single Quotes

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

3.1.2.3 Double Quotes

Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

The special parameters * and @ have special meaning when in double quotes (see Shell Parameter Expansion).

Remove redundant paths from $PATH variable

You just execute:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

that would be for the current session, if you want to change permanently add it to any .bashrc, bash.bashrc, /etc/profile - whatever fits your system and user needs.

Note: This is for Linux. We'll make this clear for new coders. (` , ') Don't try to SET = these.



Related Topics



Leave a reply



Submit