How to create and use custom shell commands?

Each of us had a situation, where we had to invoke a few, same commands each time. Making it once is not that problematic, but when we are supposed to repeat given operations, it may be better just to create one function that will handle it all.


This content originally appeared on DEV Community and was authored by Renata

Each of us had a situation, where we had to invoke a few, same commands each time. Making it once is not that problematic, but when we are supposed to repeat given operations, it may be better just to create one function that will handle it all.

Create own functions

To create our own functions, firstly we need to open a new terminal. By default, we ought to be in the root directory (~), but to be sure you can execute the following command:

cd ~

and hit enter.

Right now, we can create a new file, in which we will store our function with commands. To do it, execute this:

touch .custom_bash_commands.sh

The naming convention is up to you, in this case, the file will be named .custom_bash_commands.sh , where '.' at the beginning means it is a hidden file. To see all of the hidden and visible files, you can just do ls -a.

Once we have created the file, we can open it. To do so, you can use VIM or your favorite text editor. I will be using Visual Studio Code. For that, I will execute the following command in my current directory:

open -a 'Visual Studio Code' .custom_bash_commands.sh

Now, we can create our function by following:

#!/bin/bash

function mix_all(){
  echo "mix format"
  mix format
  echo "mix static.credo"
  mix static.credo
  echo "mix dialyzer"
  mix dialyzer
  echo "mix test"
  mix test
}

The first line is just a convention, which is used while creating Scripts. Next, we have function declaration, which is called mix_all .

We use echo command to print out the following state. Of course, this is just a sample function, you can use whatever you want to up here.

Read more...


This content originally appeared on DEV Community and was authored by Renata


Print Share Comment Cite Upload Translate Updates
APA

Renata | Sciencx (2022-07-11T19:45:26+00:00) How to create and use custom shell commands?. Retrieved from https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/

MLA
" » How to create and use custom shell commands?." Renata | Sciencx - Monday July 11, 2022, https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/
HARVARD
Renata | Sciencx Monday July 11, 2022 » How to create and use custom shell commands?., viewed ,<https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/>
VANCOUVER
Renata | Sciencx - » How to create and use custom shell commands?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/
CHICAGO
" » How to create and use custom shell commands?." Renata | Sciencx - Accessed . https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/
IEEE
" » How to create and use custom shell commands?." Renata | Sciencx [Online]. Available: https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/. [Accessed: ]
rf:citation
» How to create and use custom shell commands? | Renata | Sciencx | https://www.scien.cx/2022/07/11/how-to-create-and-use-custom-shell-commands/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.