Making my first bash script, can't get the cd command to 'stick.' [duplicate]

Solution 1:

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.