Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias with variable in bash [duplicate]

Tags:

linux

bash

People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

Do aliases work in bash scripts?

BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and flexible.

Can a bash alias take argument?

Bash users need to understand that alias cannot take arguments and parameters. But we can use functions to take arguments and parameters while using alias commands.

Should aliases go in Bash_profile or Bashrc?

As with Bash aliases in general, you can put your gc alias in ~/. bashrc or in ~/. bash_aliases but it should not go in .


I'd create a function for that, rather than alias, and then exported it, like this:

function tail_ls { ls -l "$1" | tail; }

export -f tail_ls

Note -f switch to export: it tells it that you are exporting a function. Put this in your .bashrc and you are good to go.


The solution of @maxim-sloyko did not work, but if the following:

  1. In ~/.bashrc add:

    sendpic () { scp "$@" [email protected]:/www/misc/Pictures/; }
    
  2. Save the file and reload

    $ source ~/.bashrc
    
  3. And execute:

    $ sendpic filename.jpg
    

original source: http://www.linuxhowtos.org/Tips%20and%20Tricks/command_aliases.htm


alias tail_ls='_tail_ls() { ls -l "$1" | tail ;}; _tail_ls'

You can define $1 with set, then use your alias as intended:

$ alias tail_ls='ls -l "$1" | tail'
$ set mydir
$ tail_ls

tail_ls() { ls -l "$1" | tail; }