I wonder if it is possible to set a "global" alias in bash, like zsh's -g alias option - not "global" from the user's point of view but from the shell's point of view.

What I want to know is: Can an alias (or something else?) be substituted anywhere on a line in bash?


alias ...='../..'
  • Note that ... is a perfectly valid filename, so this may not be a great idea if you have a world-writable directory (e.g. accessible via anonymous FTP). – Kevin Jul 16 '15 at 13:55
  • Thanks Kevin but that's just an exemple – maxxvw Jul 16 '15 at 21:29

From the bash(1) man page:

  Aliases allow a string to be substituted for a word when it is used  as
  the  first  word  of  a  simple command. [...]

So bash aliases do not have this capability, nor does bash have a trivial pre-exec capability (but see here for a hack though).

As a partial workaround you may be able to use a completion function, here's a minimal starting point:

function _comp_cd() {
    local cur=${COMP_WORDS[COMP_CWORD]} # the current token
    [[ $cur =~ \.\.\. ]] && {
        COMPREPLY=( $cur )
    COMPREPLY=()    # let default kick in

complete -o bashdefault -o default -F _comp_cd cd

Now when you hit tab on a cd command and the word under the cursor contains "...", each will be replaced with "../..". Completion suffers from a slight problem too though (excluding its complexity) which you can probably guess from the above, you need to specify it on a command by command basis.

The bash-completion package uses a default completion handler, with on-the-fly loading of completion functions to deal with this. If you're feeling adventurous you should be able to modify its internal function _filedir() function which is used for general file/directory expansion so as to include a similar substitution "...".

(All of which reminds of the NetWare shell, which made "..." Just Work.)


bash aliases only work based on the first word on the line and can not be defined in a global sense like zsh.

The only fudge I can think of would be to define a variable e.g.

export PP=../..  # $PP meaning parent of parent
cd $PP/anotherDirectory

But to be honest I would prefer to just type ../.. in that case. In short bash does not do it.

  • Thanks nice answer, workaround included, maybe bash should stay bash – maxxvw Jul 16 '15 at 21:34
  • not only on the first word: see my answer – Evgeny Vereshchagin Jul 20 '15 at 1:32

Bash aliases do not have this capability, but you can write:

alias cd='cd '
alias ...='../..'
cd ... # teleport to ../..


Bash Reference Manual says:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

Possible solution is a Readline's macro. You can write:

set -o emacs
bind '"\C-x...":"cd ../.."'

Type echo,Control+x,...

You should see echo cd ../..


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.