deferring argument in "alias" in .bashrc

I am a dumb user, who works with LINUX (ubuntu 14) My newly purchased laptop forces me to use the bash shell, and it has .bashrc. I used to use the C-shell before that and tried to copy its aliases from .cshrc to .bashrc with some obvious editing.

One thing doesn't work though -- the deferred argument. As an example - my .cshrc command:

alias rm 'mv -f \!* $HOME/zap' 

when written in .bshrc as

alias rm = 'mv -f \!* $HOME/zap' 

generates an error message:

bash:alias: rm: not found bash:alias: mv -f \!* $HOME/zap: not found 

(the directory zap was created by me in advance)

What should I use instead of \!*?

Replay

You should use shell functions instead of aliases in these cases. It's a bit more to type, but it allows you to be more flexible:

function rm {
  if ! test -d "$HOME/zap"; then
    echo "No zap in home" >&2
    return 1
  else
    mv -f -- "[email protected]" "$HOME/zap/"
  fi
}

The [email protected] will expand to the arguments you give rm on the command line. The -- is necessary to signal an end to command line flags (so that rm -f, where -f is a file, is possible, rm and mv don't share many command line flags anyway).

To access the original rm command, use \rm or command rm.

The testing for the existence of $HOME/zap, outputting an error message to standard error, and exiting with non-zero exit status in the function is just to show that you could do anything you'd want in there. You could obviously slim it down to just

function rm {
  mv -f -- "[email protected]" "$HOME/zap/"
}

The bash manual contains the statement:

For almost every purpose, aliases are superseded by shell functions.

Aliases are really only useful for e.g. tagging on your favourite flags to ls and things like that.

Category: bash Time: 2016-07-28 Views: 7

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.122 (s). 12 q(s)