How to Pass Arguments to Bash Alias

Last updated: March 8, 2024 | Dwijadas Dey

A bash alias is another name for a single command or a sequence of commands. You can think of bash alias as a shortcut for another long command. An alias is useful in situations where one needs to type a long sequence of commands repetitively. An alias thus helps to type and remember long commands easily. 

Often parameters or options used within the long sequence of commands are not the same every time it is executed. To make a temporary adjustment, one can update the options used in the alias definition to reflect the changes in the command and run the alias again. But this is also a repetitive task. 

So what is the ideal solution?  Is it possible to substitute parameters within the long sequence of commands with arguments from the command line while creating a bash alias? Let's find out more about the possibility of using arguments in bash alias.

Limitations of Basic Bash Aliases

To understand how the bash alias works for a simple command, we will create and use a simple bash alias before listing the limitations of the bash alias.

The syntax for creating an alias is

alias <Name of alias>="<Sequence of commands to run>" 

For example, create an alias for the command ls -ltr with the following command. 

$ alias l="ls -ltr"

Run the alias.

$ l

To remove the alias, use the following command.

$ unalias l

Proceeding forward, Let's create a simple alias to demonstrate the usage of variables in an alias.

Create a bash alias for the copy command by the name c with two variables namely $1 and $2, where $1 will be replaced by a filename(argument) and $2 will be replaced by a folder name(argument) while calling the alias from the command line.

$ alias c="cp $1 $2"

Create a test folder and a file.

$ mkdir -p test && echo "Testing bash alias!" > out.txt

Run the alias with two arguments.

$ c out.txt test

Verify if the file has been copied to the destination folder.

$ cat test/out.txt

Testing bash alias!

The above bash alias with arguments worked exactly like what it was created for.

Let us look at another example where we will create a bash alias by using a single variable to tail a log file. The variable will be substituted by an argument(line number) when the alias is executed.

$ alias t="tail -n $1 /var/log/dmesg"

 // Where $1 will be substituted by the argument from the command line.

Run the bash alias.

$ t 10
tail: invalid number of lines: ‘/var/log/dmesg’

This time the bash alias with an argument did not run successfully. To find out the actual command used for the above bash alias, type t in the terminal followed by an argument and press ctrl+alt+e. The output details the actual command executed under the hood by substituting the arguments at the end of the command.

$ tail -n  /var/log/dmesg 10 ← The argument is appended at the end of the command.

As we can see in the terminal, argument 10 is appended to the end of the command, although we expected the variable $1 will be replaced by argument 10 after the switch -n in the command. 

So we can conclude that arguments to a bash alias are always appended at the end of the command. Hence, usages of arguments with bash alias will not work always.

Mimicking Alias Behavior with Functions

Since the bash alias does not work with arguments, it is possible to mimic the behavior of the bash alias by using a function. Further, functions in bash can accept arguments from the command line. The same function can then be used as an alias in the system.

Use the following syntax to create a bash function:

function <Name of the function> {
<Commands to run>
}

Let's create a simple test function to tail a log file and run it in the terminal.

$ function tail_dmesg(){ cd /var/log; tail -f dmesg; };

Test the function in the terminal.

$ tail_dmesg

Now make an alias for the above function and run it.

$  alias t='tail_dmesg'
$ t
[    5.620375] kernel: snd_hda_codec_realtek hdaudioC0D0: autoconfig for ALC236: line_outs=1 (0x14/0x0/0x0/0x0/0x0) type:speaker
[    5.620383] kernel: snd_hda_codec_realtek hdaudioC0D0:    speaker_outs=0 (0x0/0x0/0x0/0x0/0x0)
[    5.620386] kernel: snd_hda_codec_realtek hdaudioC0D0:    hp_outs=1 (0x21/0x0/0x0/0x0/0x0)
[    5.620388] kernel: snd_hda_codec_realtek hdaudioC0D0:    mono: mono_out=0x0
…
…

In the above example, we have seen how a long sequence of commands can be encapsulated within a function and impersonate the behavior of an alias. Let's look at the usages of arguments in a function in the next section.

Handling Arguments in Functions

To create a bash function using arguments, variables like $1, $2, and so on are used depending on the number of arguments needed in the command. Let's use variables to implement the same function we created in the previous section. 

$ function tail_dmesg(){ cd "$1"; tail -f "$2"; };

Now call the function by passing two arguments for the variables $1 and $2

$ tail_dmesg /var/log dmesg

To create a function and make an alias altogether in a single command, use the following command.

$ alias t='function tail_dmesg(){ cd "$1"; tail -f "$2"; }; tail_dmesg'

The above-listed commands show how to create a bash function using arguments. The benefits of using a bash function are immense for a long sequence of commands with several options since it eliminates the need to type a repetitive command with a single word or even with a letter.

Update Bash configuration

To make the bash alias permanent in the bash environment so that it is available for every new bash session, edit the file .bashrc in the user home directory and append the alias/function definition. 

$ vi .bashrc
…
…
alias t='function tail_dmesg(){ cd "$1"; tail -f "$2"; }; tail_dmesg'
…
…

Reload the bash environment.

$ source .bashrc

Verify

Verify the new bash settings by launching a new bash environment and running the alias defined in the file .bashrc

$ t /var/log dmesg
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment