(1) Use any editor like vi or mcedit to write shell script.
(2) After writing shell script set execute permission for your script as follows
chmod permission your-script-name
$ chmod +x your-script-name
$ chmod 755 your-script-name
Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).
(3) Execute your script as
$ bash bar
$ sh bar
NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows
$ . foo
Now you are ready to write first shell script that will print "Knowledge is Power" on screen.
After saving the above script, you can run the script as follows:
This will not run script since we have not set execute permission for our script first; to do this type command
$ chmod 755 first
First screen will be clear, then Knowledge is Power is printed on screen.
$ vi first
Start vi editor
# followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script.
clear the screen
echo "Knowledge is Power"
To print message or value of variables on screen, we use echo command, general form of echo command is as follows
(My own bin directory to execute script)
Tip: For shell script file try to give file extension such as .sh, which can be easily identified by you as shell script.
1)Write following shell script, save it, execute it and note down the it's output.
| || |
| || || |
Variables in Shell