How to create executable from everywhere bash script in Devuan? [with screenshots] [ex.#1]

Written by Administrator on Wednesday April 22, 2020

Bash scripts are very helpful took for work routine and simplifying your work with linux.

Let's create a simple bash script, which writes something in terminal.

#!/bin/bash

for i in  {1..10}
do
  echo Hello number $i
  sleep 3s
done

What does this script? Echo command prints "Hello number #" precisely ten times, but with delay 3 seconds.

Save the code as delayed_hello.sh:

dima@computer:~/scripts$ cd /home/dima/scripts
dima@computer:~/scripts$ ls
delayed_hello.sh

Making bash script executable

$ chmod +x delayed_hello.sh chmod +x delayed_hello.sh

Now we can run this script:

To run the script, enter the directory, where script is stored (in my case it is: /home/dima/scripts) and run it the way you see in code example below:

$ ./delayed_hello.sh

output:

Hello number 1
Hello number 2
Hello number 3
Hello number 4
Hello number 5
Hello number 6
Hello number 7
Hello number 8
Hello number 9
Hello number 10

Make bash script executable from everywhere in Devuan system

We have created script and we know, how to run it. Sometimes it may be helpful to run the script from everywhere in your system, meaning regardless the folder you are currently in.

To do this, open a file /home/username/.bashrc   .You can use every text editor you like.

Using vim:

$ sudo vim ~/.bashrc

Using nano:

$ sudo nano ~/.bashrc

and add following line in the code:

alias delayed_hello='/home/dima/scripts/delayed_hello.sh'

So, you see, we are defining alias name (the name of the script, we will be calling it by, and then the absolute path to our script).

adding alias to bash script in Devuan

Save the file.

Now - update bashrc:

$ source ~/.bashrc

Running the script

Now we can run the script from everywhere in system and use only the alias name of our script (delayed_hello), without remembering the path to our script. Just type the alias name of the script:

calling bash script alias to run the script

Category: linux Tags: bash devuan scripting nano vim