11

For my first bash script, I want to create something that's really been annoying me: when I switch folders, I want the contents of that folder to be displated automatically. I tried adding this following code to ~/.bashrc:

alias go='cd; ls'

Simple enough I thought! Not so. While typing go /etc does indeed list the contents of /etc, my working directory hasn't actually changed, I'm still in the one I was in before. How do I remedy this?

glenn jackman
  • 17,625
  • 2
  • 37
  • 60
Ram
  • 219
  • 2
  • 6

2 Answers2

29

In your example, go /etc will do cd; ls /etc. That means, first, cd will change the current directory to your home directory. Then, ls /etc will display the contents of /etc.

You could achieve what you want by defining a function, like so:

function go() {
    cd "$1" && ls
}

Or just type it in the command line on a single line:

function go() { cd "$1" && ls; }

Then go /etc will do what you want.

$1 refers to the first parameter passed to the command in this example /etc. You can refer to subsequent parameters with $2, $3 and so on.

David Richerby
  • 296
  • 3
  • 10
Timo Kluck
  • 8,753
  • 1
  • 20
  • 18
  • 3
    +1. One improvement: only execute `ls` if `cd` is successful: `go() { cd "$1" && ls; }` -- this avoids listing the current directory if the parameter does not exist. – glenn jackman Aug 11 '14 at 19:10
  • This seems to have done the trick. While it doesn't look complicated relatively speaking, it turns out it was still a bit more complicated than I had initially intended. One quick question, though: how does the "$1" work? – Ram Aug 11 '14 at 19:15
  • 2
    @user3471004 - `$1` first argument passed to command - this also follows on with `$2` for the second, `$3` for the third, etc. `$@` can also be used to represents all of the arguments. Various examples can be found - e.g [this](http://how-to.wikia.com/wiki/How_to_read_command_line_arguments_in_a_bash_script). – Wilf Aug 11 '14 at 20:12
-1

You may want to combine this with the built-in bash directory stack (dirs). this will give you the opportunity to type: go ..., to view the previous folder(s) in your stack, rather then type their name. an eg:

function go() { 
 if [ "$1" == "..." ]; then popd >/dev/null ; else pushd "$1" >/dev/null ; fi
 ls $@
}

you can substitute ... with other keyword, like _back. something that wont be a directory name.

you will notice the ls $@ which means all remaining parameters will be passed to ls. so if you want to go and have long listing, or reverse time listing, use: go /var -l or go /etc -ltr

CorneC
  • 1
  • As you do not remove `$1` from `$@` it will still be the first argument for `ls $@`. In most cases this will lead to an error when using relative paths. – Adaephon Aug 12 '14 at 08:48
  • Not particularly relevant to the question. – moopet Aug 12 '14 at 11:20
  • The `$@` should be quoted in case any of the original terms contain spaces. Hence it should be `ls "$@"` (or with best practice `ls "${@}"`). – Paddy Landau Aug 19 '14 at 10:47