breaking into the unknown…

sh command in linux ubuntu

Leave a comment

While, trying out rails deployment with Capistrano, I have seen use of sh multiple time in the Capistrano terminal log. To get some detail on it, I searched on Google and find very good documentation here. Technically, sh command executes the Bourne shell, likely taking you to a $ prompt.The simplest definition I will give is that “sh command execute terminal command from any file with .sh extension”

=> Example:

Create a file with .sh extension, say

Add below lines to the file

#!/bin/sh # this is the default location, so you can ignore it, but mention it if your sh script is located at some other place. As a good practice always mention it

mkdir my_folder #it will create my_folder directory
cd my_folder # will take you to my_folder directory
mkdir my_notes #again make a directory my_notes within my_folder directory
cd my_notes #take you to my_notes folder
touch first_note.txt #create a first_notes.txt file within my notes directory
echo “see your sh example is working” >> first_note.txt # add a line to the first_notes file
cat first_note.txt #print the content of first_notes.txt on the terminal

Now go terminal and run the below command

arun@arun-yadav:~$ sh
see your sh example is working # this is the result of last command in our file

Now, if you check your folders, you will see all the newly created content

=> Running a .sh file

.sh file is nothing but the shell script to install given application or to perform other tasks under UNIX like operating systems. When we download any software, we find file which basically conten a set of command to be exicuted on the terminal. We can run any .sh file in following way

-> $ sh

-> $ bash # sh is also a bash script, so this will also work

-> $ ./ #this will work if the have executable permission, you can grant it with chmod +x command, if not already there.more detail on managing file permission is available here

-> $ sudo bash # above will not work if, the file need to be executed by root user, in that case use this command, alternatively you can login as root user with su – command and use any of the above method

=> sh options

You can, provide a no of options to sh to suit your need. The complete list is available here. A sh command with all the options will look something like this

sh [-a] [-c] [-C] [-e] [-E] [-f] [-h] [-i] [-I][-k] [-m] [-n] [-p] [-r] [-s] [-t] [-T] [-u] [-v] [-x] [ argument ]

Explanation of some most commonly used options is  is as below:

-c string #If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.


$ sh -c “mkdir my_folder && cd my_folder && touch my_notes.txt”

-C    #Don’t overwrite existing files with “>.”

-i # If the -i option is present, the shell is interactive.

-I # Make bash act as if it had been invoked as a login shel

-s #If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.

[argument] # this is basically a string of command when you are using -c option or the value you pass, interactively by using -s option. If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits. Bash’s exit status is the exit status of the last command executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open the file in the current directory, and, if no file is found, then the shell searches the directories in PATH for the script.

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s