How to Create Shell Functions in Linux

How to Create Shell Functions in Linux

Finding out how to create shell functions in Linux can allow you to simplify even the most complex lines of code in an instant. Here’s everything you need to know about it.

A collection of commands arranged by common functionality is called a function, and it is a powerful element of shell programming. These simple units just produce a single value and are fairly easy to maintain when called.

Shell functions and aliases are different on two counts. First is that aliases do not take arguments the way functions do. Moreover, if a command name is defined as a function as well as an alias, the alias always takes precedence. 

However, more sophisticated functions that can accept a range of parameters and return values are required when developing software. We will talk about these functions in this section.

How to Create Shell Functions in Linux

In some cases, you may require an alias that can accept one or more arguments, which is when shell functions come into play. Functions have a significantly greater set of features than aliases. We can employ loops and conditions inside of functions since they are more programmatic. 

The ability to send arguments to functions is crucial, and the ability to export functions is another essential feature. By doing this, they become available to shell scripts running in the environment where they were defined. 

Syntax and Process

Creating a shell function is not as simple as creating aliases but is not that difficult either. You can declare them as:

function_name () { command1; command2; }

You can define shell functions with the “.bashrc” file, similar to what we did for aliases. But the better way is to use their own definitions file. You can call it “.bash_functions”, since the convention is used for the “.bash_aliases” file.

Hence, you must tell the “.bashrc” file to read the definitions of your input. All you need to do is copy and amend the “.bash_aliases” file code snippet. For this, you can begin by launching gedit and loading the “.bashrc” file with the following command: 

.bashrc

Now we need to change the texts from “.bash_aliases” all the way to “.bash_functions” so proceed with the command as shown below: 

.bash_functions

We can now close saving those changes. 

Go ahead and begin creating and editing the “.bash_functions” file. Then, place a function definition in the file. 

How to Create Shell Functions in Linux


The empty “.bash_functions” file in gedit will be opened by this action.

Then, you need to add the “up” function, which will form a single command line parameter in digits. So, if we use the following command: 

up 2

Entering the up command will call ‘cd ..’ twice and then move up two levels in the directory tree. Now, to define a function, you can use the following command:

function up() {

Remember, the word “function” is always optional. 

How to Create Shell Functions in Linux

We use the above format where function up() { is a marker for the start of the function definition. And names the function “up”. levels=$1 forms a variable named “levels” and the value is to that of the first parameter. This parameter will be a digit you have given when you – the user – call upon the function. 

The $1 defines the “first command line parameter”.

The line with while is about the loop, which translates as “while the value of levels is more than zero, do what is contained in the loop body.” Within the body of the loop, cd .. means to move one level above in the directory tree. And levels=$(($levels – 1)) set a new value for the levels, being one less than the current value.

Now, you need to head back to the top of the loop, where you must compare the value of levels and zero again. If the level is above zero, execution of the loop’s body is done again. If the value is not higher than zero, the loop is then finished, and you drop through to the done statement, completing the function.

After saving the changes and closing the gedit, you need to execute the commands in “.bashrc”. And that must read in and proceed with executing the commands in the “.bash_functions” file.

You can now test the function by relocating within the directory and using “up” to go back to a “higher” point within the directory tree. 

How to Create Shell Functions in Linux
How to Create Shell Functions in Linux

Finding Alias or Function with type Command

If you are writing a long piece of code over several days or weeks, it can be a nightmare to remember if a specific command is either an alias or a function. We can utilize the “type” command for remembering its type. This can be done just by using the type command:

type ftc
type up

In the article above, we have defined the difference between aliases and functions, and why functions tend to offer more complex features than the latter. We have also discussed, in detail, the process of creating shell functions and Linux, and at the end, how to differentiate between an alias and a function. 

If you have any questions regarding this topic, do not hesitate to leave us a comment below.

If this guide helped you, please share it.

Leave a Reply
Related Posts