Hot answers tagged

377

The simple stuff PATH=$PATH:~/opt/bin PATH=~/opt/bin:$PATH depending on whether you want to add ~/opt/bin at the end (to be searched after all other directories, in case there is a program by the same name in multiple directories) or at the beginning (to be searched before all other directories). You can add multiple entries at the same time. ...


64

You have to put the declaration in the initialization files of your shell: If you are using bash, ash, ksh or some other Bourne-style shell, you can add ABC="123"; export ABC in your .profile file (${HOME}/.profile). This is the default situation on most unix installations, and in particular on Debian. If your login shell is bash, you can use ...


64

Add cd /home/xxxx/Documents/Scripts/ if you want your job to run in that directory. There's no reason why cron would change to that particular directory. Cron runs your commands in your home directory. As for ssmtp, it might not be in your default PATH. Cron's default path is implementation-dependent, so check your man page, but in all likelihood ssmtp is ...


45

Either way works, but they don't do the same thing: the elements of PATHare checked left to right. In your first example, executables in ~/opt/bin will have precedence over those installed, for example, in /usr/bin, which may or may not be what you want. In particular, from a safety point of view, it is dangerous to add paths to the front, because if ...


44

You can always do: sudo env "PATH=$PATH" godi_console or even: sudo "PATH=$PATH" godi_console as sudo does treat leading arguments containing = characters as environment variable assignments by itself as well. As a security measure on Debian, /etc/sudoers has the secure_path option set to a safe value.


33

You can use tr. $ tr ':' '\n' <<< "$PATH" /Users/arturo/.rvm/gems/ruby-1.9.3-p392/bin /Users/arturo/.rvm/gems/ruby-1.9.3-p392@global/bin /Users/arturo/.rvm/rubies/ruby-1.9.3-p392/bin ... You can also do this in some shells (tested in bash and zsh): echo -e ${PATH//:/\\n} In zsh, you can use the $path variable to see your path with spaces ...


30

Grepping around in /etc turned up a link that Googling did not. It turns out you can control this in the file /etc/fstab. Just add a line that says none / cygdrive binary 0 0 and the problem should be fixed. No more kludgey fixes in .bashrc, and no messed-up $PATH.


30

The shared library HOWTO explains most of the mechanisms involved, and the dynamic loader manual goes into more detail. Each unix variant has its own way, but most use the same executable format (ELF) and have similar dynamic linkers (derived from Solaris). Below I'll summarize the common behavior with a focus on Linux; check your system's manuals for the ...


28

The :+ is a form of parameter expansion: ${parameter:+[word]} : Use Alternative Value. If parameter is unset or null, null shall be substituted; otherwise, the expansion of word (or an empty string if word is omitted) shall be substituted. In other words, if the variable $var is defined, echo ${var:+foo} will print foo and, if it is not, it ...


27

If you're the only user on the machine it's OK, as long as you know what's you're doing. The general concern is that by having your current directory in PATH, you cannot see commands as a constant list. If you need to run script/program from your current directory, you can always explicitly run it by prepending ./ to its name (you saying the system "I want ...


25

Note the output here: root:/usr/local/bin# siege bash: /usr/bin/siege: No such file or directory Bash maintains an internal hash of previously found executables in your path. In this case, it has details that at one time there was an executable at /usr/bin/siege, and reuses that path to avoid having to search again. You need to tell bash to manually ...


25

Having more entries in $PATH doesn't directly slow your startup, but it does slow each time you first run a particular command in a shell session (not every time you run the command, because bash maintains a cache). The slowdown is rarely perceptible unless you have a particularly slow filesystem (e.g. NFS, Samba or other network filesystem, or on Cygwin). ...


24

Instead of using which, which doesn't work when you need it most, use type to determine what will run when you type a command: $ which set ./set $ type set set is a shell builtin The shell always looks for builtins before searching the $PATH, so setting $PATH doesn't help here. It would be best to rename your executable to something else, but if your ...


23

Suppose that the new path that we want to add is: new=/opt/bin Then, using any POSIX shell, we can test to see if new is already in the path and add it if it isn't: case ":${PATH:=$new}:" in *:$new:*) ;; *) PATH="$new:$PATH" ;; esac Note the use of colons. Without the colons, we might think that, say, new=/bin was already in the path because ...


23

The risk is someone put a malicious executable in the directory that happen to be your current one. The worst case happen when: you are logged as root as the malicious command has unlimited damage power . is at the beginning of your PATH as standard commands can be overridden without you noticing it (typically an ls which could hide itself from the list). ...


22

In zsh $PATH is tied (see typeset -T) to the $path array. You can force that array to have unique values with: typeset -U path And then, add the path with: path+=(~/foo) Without having to worry if it was there already. To add it at the front, do: path=(~/foo "$path[@]") or: path[1,0]=~/foo if ~/foo was already in $path that will move it to the ...


21

Probably to keep the kernel simpler. I don't think the kernel ever searches your path to find an executable. That's handled by the C library. #! processing is done in the kernel, which doesn't use the standard C library. Also, I don't think the kernel has a notion of what your path is. $PATH is an environment variable, and only processes have an ...


20

You cannot solve this through shebang directly, since shebang is purely static. What you could do is having some »least common multiplier« (from a shell perspective) in the shebang and re-execute your script with the right shell, if this LCM isn't zsh. In other words: Have your script executed by a shell found on all systems, test for a zsh-only feature ...


18

I've seen people clean up duplicates from their PATH variable using awk and something like this: PATH=$(echo "$PATH" | awk -v RS=':' -v ORS=":" '!a[$1]++') You could try adding that to your own bashrc and make sure you source the other files somewhere before running that. An alternative would be to use the pathmearge utility. As for your speed problem, ...


17

On linking You generally do not link /usr/local/* with /bin, but this is more of a historical practice. In general, there are a few "technical" reason why you cannot do what you're suggesting. Making links to executables in /bin can cause problems: Probably the biggest caveat would be if you're system is having packages managed by some sort of package ...


16

Use /etc/environment file for setting the environment variables. Then add the following line inside the /etc/environment file. ABC="123" Now the ABC variable will be accessible from all the user sessions. To test the variable output first refresh the environment variable using command source /etc/environment and run echo $ABC.


16

You have to use source or eval or to spawn a new shell. When you run a shell script a new child shell is spawned. This child shell will execute the script commands. The father shell environment will remain untouched by anything happens in the child shell. There are a lot of different techniques to manage this situation: Prepare a file sourcefile containg ...


16

Not an interesting solution at all, but very portable: PATH=${PATH}:/my/path/1 PATH=${PATH}:/my/path/2 PATH=${PATH}:/my/path/3


15

I'm confused by question 2 (since removed from the question since it was due to an unrelated issue): What's a workable way to append more paths on different lines? Initially I thought this could do the trick: export PATH=$PATH:~/opt/bin export PATH=$PATH:~/opt/node/bin but it doesn't because the second assignment doesn't only append ...


15

Take a look at /etc/sudoers. The default file in Fedora (as well as in RHEL, and also Ubuntu and similar) includes this line: Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin Which ensures that your path is clean when running binaries under sudo. This helps protect against some of the concerns noted in this question. It's also convenient if you ...


15

In the most common cases, $0 will contain a path, absolute or relative to the script, so script_path=$(readlink -e -- "$0") (assuming there's a readlink command and it supports -e) generally is a good enough way to obtain the canonical absolute path to the script. $0 is assigned from the argument specifying the script as passed to the interpreter. For ...


14

That's not the Bourne shell, or bash emulating the Bourne shell, that's the Almquist shell, in your case probably the Debian Almquist shell (a Linux fork by Debian of BSDs' sh itself based on the original Almquist shell). In the Almquist shell (the original one and the modern versions), % is used in PATH for extra features specific to ash. Quoting from the ...


14

On some systems, which -a shows all matches. If your shell is bash or zsh¹, you can use type instead: type foo shows the first match and type -a foo shows all matches. The three commands type, which and whence do mostly the same thing; they differ between shells and operating systems in availability, options, and what exactly they report. type is always ...


14

That line in your .profile should be one of export PATH="$PATH:$HOME/Unix/homebrew/bin" PATH="$PATH:$HOME/Unix/homebrew/bin" PATH=$PATH:$HOME/Unix/homebrew/bin PATH=$PATH:~/Unix/homebrew/bin The ~ character is only expanded to your home directory when it's the first character of a word and it's unquoted. In what you wrote, the ~ is between double quotes ...


14

PATH is for specifying directories of executable programs. LD_LIBRARY_PATH is used to specify directories of libraries.



Only top voted, non community-wiki answers of a minimum length are eligible