2

I'm using a bash shell and I want to make an alias that will do a couple things to a given directory. Let's say I want the alias to be "execute" and I want it to ls and cd. I want "execute" to take a single argument, which is a directory that it will then ls, and then cd into.

execute temp

should be the equivalent of:

ls temp ; cd temp

So how can I create an alias for execute that reads the next input after execute, and then use that as an argument for the other two? Something like this?

alias execute="directory=VALUE ; ls $directory ; cd $directory"
1
  • 1
    you can't give arguments (regardless of how you try to hide it) to an alias. Some shell's alias allow you to append something at the end by defining the alias as alias tstR='doSomething ' (note the trailing space) which will set up the cmd line with what ever you type after tstR. Read about shell functions, they are designed to process arguments. Good luck. Commented Feb 12, 2014 at 4:17

1 Answer 1

7

Functions are more suitable for such tasks:

execute() {
  ls "$1"; cd "$1";
}

Invoke it by saying execute temp.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, I normally put above functions in .profile or .bashrc, so when login, I have it immediately. And this is the magic and secret when someone else asks why I don't have this command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.