1

How can I alias ... to ../.. in Bash?

I am aware that other answers allow alias '...'='cd ../..' but I'd like to be able to address the directory two levels up with other commands, allowing for:

cd ...
ls ...
realpath ...

I have tried alias '...'='../..' but when trying to use this I get the following:

$ alias '...'='../..'
$ type ...
... is aliased to `../..'
$ cd ...
bash: cd: ...: No such file or directory
Anil
  • 1,097
  • 7
  • 20

2 Answers2

2

You can define functions, example for cd:

cd() { [[ $1 == ... ]] && command cd ../.. || command cd "$@"; }

You can put it in ~/.bashrc and source it: source ~/.bashrc

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

Gilles answer allows you to apply ... by checking if the first param equals ... and then hardcoding the paramter.


You could reverse the command, where you pass the command you wish to run with ../..:

function ...() {
    [[ -z "$1" ]] && printf "No command given\n" && return 0;
    $@ ../..
}

Usage:

... cd
... ls -lta
0stone0
  • 34,288
  • 4
  • 39
  • 64