This question already has an answer here:

I use aliases a lot but right now only for use cases like alias i='sudo apt-get install -y'. I often would like to add an alias in the following form:

alias cmd='echo [something] >> /path/to/file' where I would like to substitute [something] with what I enter after the cmd.

I can obviously create a one-line script,save it somewhere and make an alias to that command but since I only want to substitute only 1 word in a pipe, is there a simpler way to do this?

marked as duplicate by don_crissti, Gilles command-line Dec 20 '14 at 20:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Functions are perfectly suitable for this purpose. For example:

cmd() { echo $* >> /path/to/file'; }

This is on one line, just like an alias. But it can take parameters.

  • Does the $* mean to take all of the command line arguments supplied? – syntagma Dec 20 '14 at 20:20
  • Yes. You can use $1 if you want only the first argument. – janos Dec 20 '14 at 20:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.