How to make a file executable (script) in Linux

How to make a file executable (script) in Linux

1. open your command line, Terminal 

In Linux Mint OS, I open the terminal command line by clicking on the LM logo (on the top right corner) and it will show the search bar and I type terminal on that search bar and click on the terminal apps. as shown on image below:

2. In the terminal prompt I type the word echo hello. Echo is build in linux command to echo the word that you type as shown below

3. I will make a script (an executable file) name “myfirstscript”, that when I type the script $ myfirstscript will echo the word hello

4. use a vi editor or nano or a text editor to make a file and input the command: echo hello, as shown below.

Because I use vi editor, after i press enter, it will show the content of the file as shown below.

It will have nothing in its content because it is a new file.

to start typing using vi editor, i will need to press the keyboard key "i" means insert and type the word echo hello as shown below

Note: if making a mistake in typing in vi editor, I use the "Backspace" to go back and type the word correctly again. or I can press "Esc" and press x and it will delete the character one by one until it reaches the mistaken character and as said above that to start typing again press the keyboard key "i" and start typing the correct word.

To exit and save the file, I need to press the keyboard key "Esc" and type :wq! and enter. it will go out from vi editor

5. When done saving the file to make the file executable by executing chmod 700 myfirstscript as shown below.

 

6. to execute the file just type the name of the script file $ ./myfirstscript as shown below.

    

As far as making script it's done, but if you don't want to always type the "./" in front of the your script file every time you execute your script, you need to add in your ".bashrc" file export PATH="~/Documents/sample:$PATH" 

and when done edit, type the word source ~/.bashrc, and you don't need anymore to use the "./" as shown below

Thank you.

Comments

Popular posts from this blog

Copy to clipboard from command line

login website quicker using linux command line

Ansible