bluetickk
bluetickk

Reputation: 689

Bash Alias Help

I am writing 4 aliases for a project and currently two of them are working. The e and ll alias work but the cx alias gives me an error cx: command not found. also, my rm alias just removes the file specified but does not give any sort of confirmation.

Here is what I am looking for from these two aliases..

  1. an alias named "cx" which accepts one or more files/directories and adds the execute permission on the specified files/directories.

  2. an alias called "rm" that will display a confirmation message each time you remove a file.

here are my aliases...

alias e="exit"
alias ll="ls $1 -l"
alias cx="chmod a+x $1"
alias rm="rm -i"

Upvotes: 0

Views: 1043

Answers (3)

Jim Lewis
Jim Lewis

Reputation: 45105

The command shopt -s expand_aliases will allow alias expansion in non-interactive Bash shells. (It's not needed at the command line, since that would be an interactive shell.)

If you're testing your aliases in a script, that's probably part of your problem. Try adding the shoptcommand to your script before attempting to run any aliased commands.

Upvotes: 0

SiegeX
SiegeX

Reputation: 140417

When you want to use passed in parameters, you don't want an alias, you want a function

cx(){ chmod a+x $1; }
ll(){ ls $1 -l; }

In these particular cases, you can do without the positional parameters altogether because the parameter is at the very end and aliases are essentially inline replaced with their definition.

alias ll='ls -l'
alias cx='chmod a+x'

So cx ./foo is now chmod a+x ./foo and ll ./foo is now ls -l ./foo

Proof of Concept

$ touch foo && ls -l ./foo
-rw-rw-r-- 1 siegex siegex 0 Mar  7 12:14 ./foo
$ alias cx='chmod a+x'
$ alias ll='ls -l'
$ cx ./foo
$ ll ./foo
-rwxrwxr-x 1 siegex siegex 0 Mar  7 12:14 ./foo

Upvotes: 4

LiorK
LiorK

Reputation: 1680

Try this, it should be better :)

alias e='exit'
alias ll='ls $1 -l'
alias cx='chmod a+x $1'
alias rm='rm -i'

The magic is ' instead of "

Upvotes: 0

Related Questions