Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, January 7, 2016

Make bash alias that takes parameter?

Make bash alias that takes parameter?


I used to use CShell (), which let you make an alias that takes a parameter. The notation was something like

alias junk="mv \\!* ~/.Trash"  

In Bash this does not seem to work. Given that Bash has a multitude of useful features, I would assume that this one has been implemented but I am wondering how.

Answer by arunkumar for Make bash alias that takes parameter?


Bash alias does not directly accept parameters. You will have to create a function and alias that.

alias does not accept parameters but it can alias a function that does. For example:

myfunction() {      #do things with parameters like $1 such as      mv $1 $1.bak      cp $2 $1  }  alias myname=myfunction  

By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this

$ myfunction original.conf my.conf  

Answer by Mike Gleason for Make bash alias that takes parameter?


Refining the answer above, you can get 1-line syntax like you can for aliases, which is more convenient for ad-hoc definitions in a shell or .bashrc files:

bash$ myfunction() { mv "$1" "$1.bak"; cp "$2" "$1"; }    bash$ myfunction original.conf my.conf  

Don't forget the semi-colon before the closing right-bracket. Similarly, for the actual question:

csh% alias junk="mv \\!* ~/.Trash"    bash$ junk() { mv "$@" ~/.Trash/; }  

Or:

bash$ junk() { for item in "$@" ; do echo "Trashing: $item" ; mv "$item" ~/.Trash/; done; }  

Answer by aliteralmind for Make bash alias that takes parameter?


Here's are three examples of functions I have in my ~/.bashrc, that are essentially aliases that accept a parameter:

#Utility required by all below functions.  #http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable#comment21953456_3232433  alias trim="sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'"  

.

:<

.

:<

.

:< /tmp/history            #Clear all items in the current sessions history (in memory). This          #empties out $HISTFILE.          history -c            #Overwrite the actual history file with the temp one.          mv /tmp/history "$HISTFILE"            #Now reload it.          history -r "$HISTFILE"     #Alternative: exec bash      else          echo "Cancelled."      fi  }  

References:

Answer by osirisgothra for Make bash alias that takes parameter?


NB: In case the idea isn't obvious, it is a bad idea to use aliases for anything but aliases, the first one being the 'function in an alias' and the second one being the 'hard to read redirect/source'. Also, there are flaws (which i thought would be obvious, but just in case you are confused: I do not mean them to actually be used... anywhere!)

................................................................................................................................................

I've answered this before, and it has always been like this in the past:

alias foo='__foo() { unset -f $0; echo "arg1 for foo=$1"; }; __foo()'  

which is fine and good, unless you are avoiding the use of functions all together. in which case you can take advantage of bash's vast ability to redirect text:

alias bar='cat <<< '\''echo arg1 for bar=$1'\'' | source /dev/stdin'  

They are both about the same length give or take a few characters.

The real kicker is the time difference, the top being the 'function method' and the bottom being the 'redirect-source' method. To prove this theory, the timing speaks for itself:

arg1 for foo=FOOVALUE   real 0m0.011s user 0m0.004s sys 0m0.008s  # <--time spent in foo   real 0m0.000s user 0m0.000s sys 0m0.000s  # <--time spent in bar  arg1 for bar=BARVALUE  ubuntu@localhost /usr/bin# time foo FOOVALUE; time bar BARVALUE  arg1 for foo=FOOVALUE   real 0m0.010s user 0m0.004s sys 0m0.004s   real 0m0.000s user 0m0.000s sys 0m0.000s  arg1 for bar=BARVALUE  ubuntu@localhost /usr/bin# time foo FOOVALUE; time bar BARVALUE  arg1 for foo=FOOVALUE   real 0m0.011s user 0m0.000s sys 0m0.012s   real 0m0.000s user 0m0.000s sys 0m0.000s  arg1 for bar=BARVALUE  ubuntu@localhost /usr/bin# time foo FOOVALUE; time bar BARVALUE  arg1 for foo=FOOVALUE   real 0m0.012s user 0m0.004s sys 0m0.004s   real 0m0.000s user 0m0.000s sys 0m0.000s  arg1 for bar=BARVALUE  ubuntu@localhost /usr/bin# time foo FOOVALUE; time bar BARVALUE  arg1 for foo=FOOVALUE   real 0m0.010s user 0m0.008s sys 0m0.004s   real 0m0.000s user 0m0.000s sys 0m0.000s  arg1 for bar=BARVALUE  

This is the bottom part of about 200 results, done at random intervals. It seems that function creation/destruction takes more time than redirection. Hopefully this will help future visitors to this question (didn't want to keep it to myself).

Answer by Amine Hajyoussef for Make bash alias that takes parameter?


An alternative solution is to use marker, a tool I've created recently that allows you to "bookmark" command templates and easily place cursor at command place-holders:

commandline marker

I found that most of time, I'm using shell functions so I don't have to write frequently used commands again and again in the command-line. The issue of using functions for this use case, is adding new terms to my command vocabulary and having to remember what functions parameters refer to in the real-command. Marker goal is to eliminate that mental burden.

Answer by Micah for Make bash alias that takes parameter?


If you're looking for a generic way to apply all params to a function, not just one or two or some other hardcoded amount, you can do that this way:

#!/usr/bin/env bash    # you would want to `source` this file, maybe in your .bash_profile?  function runjar_fn(){      java -jar myjar.jar "$@";  }    alias runjar=runjar_fn;  

So in the example above, i pass all parameters from when i run runjar to the alias.

For example, if i did runjar hi there it would end up actually running java -jar myjar.jar hi there. If i did runjar one two three it would run java -jar myjar.jar one two three.

I like this $@ - based solution because it works with any number of params.

Answer by Evan Langlois for Make bash alias that takes parameter?


The question is simply asked wrong. You don't make an alias that takes parameters because alias just adds a second name for something that already exists. The functionality the OP wants is the function command to create a new function. You do not need to alias the function as the function already has a name.

I think you want something like this :

function trash() { mv $@ ~/.Trash; }  

That's it! You can use parameters $1, $2, $3, etc, or just stuff them all with $@


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.