Setting Up Permissions on a Script
The chmod (change mode) command is a shell command in Linux. It is used to change the file system modes of files and directories, which include permissions and special modes. Each shell script must have execute permission. The mode can be either a symbolic representation of changes to make or an octal number representing the bit pattern for the new mode bits.
Examples
- Allowing everyone to execute the script, enter:
- chmod +x script.sh
(OR)
- chmod 0766 script.sh
- Only allow owner to execute the script,
- chmod 0700 script.sh
(OR)
- chmod u=rwx,go= script.sh
(OR)
- chmod u+x script.sh
- To view the permissions, use:
- ls -l script.sh
- Remove read and execute permission for the group and user
- chmod ug= script.sh
- More about chmod
- Type the following command to read chmod man page:
- man chmod
Execute a script:
A shell script can be executed using the following syntax:
- chmod +x script.sh
- ./script.sh
The Shell Variables and Environment
- Variables in shell
- You can use variables to store data and configuration options. There are two types of variable as follows:
- System Variables
- Created and maintained by Linux bash shell itself. This type of variable (with the exception of auto_resume and histchars) is defined in CAPITAL LETTERS. You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG,HISTSIZE,and DISPLAY etc.
- View All System Variables
- To see all system variables, type the following command at a console / terminal:
env
Sample Outputs from env command:
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]=”3″ [1]=”2″ [2]=”39″ [3]=”1″ [4]=”release”
[5]=”i486-pc-linux-gnu”)
BASH_VERSION=’3.2.39(1)-release’
COLORTERM=gnome-terminal
COLUMNS=158
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-FSGj0JzI4V,guid=7f59a3dd0813f52d6296ee40
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
EUID=1000
GDMSESSION=gnome
HOME=/home/vivek
HOSTNAME=vivek-desktop
HOSTTYPE=i486
IFS=$’ \t\n’
PPID=7542
PS1=’${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ‘
SHELL=/bin/bash
Etc……..
How Do I Display The Value Of a Variable?
Use echo command to display variable value. To display the program search path, type:
echo “$PATH”
To display your prompt setting, type:
echo “$PS1”
- All variable names must be prefixed with $ symbol, and the entire construct should be enclosed in quotes. Try the following example to display the value of a variable without using $ prefix:
echo “HOME” - To display the value of a variable with echo $HOME:
echo “$HOME”
You must use $ followed by variable name to print a variable’s contents. - The variable name may also be enclosed in braces:
- echo “${HOME}”
This is useful when the variable name is followed by a character that could be part of a variable name:
echo “${HOME}work”
- User Defined Variables
Created and maintained by user. This type of variable defined may use any valid variable name, but it is good practice to avoid all uppercase names as many are used by the shell.
Assign values to shell variables
Creating and setting variables within a script is fairly simple. Use the following syntax:
varName=someValue
someValue is assigned to given varName and someValue must be on right side of = (equal) sign. If someValue is not given, the variable is assigned the null string.
How Do I Display The Variable Value?
You can display the value of a variable with echo $varName or echo ${varName}:
echo “$varName”
Conclusion
Linux shell scripting is a fundamental skill that empowers you to automate tasks, manage systems efficiently, and write your own tools tailored to your workflow. By understanding how to create, edit, and execute shell scripts—along with concepts like the shebang line and proper commenting—you lay a strong foundation for system-level programming and automation.
Whether you’re a beginner learning to print “Hello, World!” or an aspiring system admin writing advanced scripts, mastering shell scripting opens up a powerful layer of control over Linux environments. Keep practicing, experiment with real-time scripts, and remember—great scripts don’t just work, they’re also readable and well-documented.
If you’re eager to deepen your understanding of advanced C++ and gain practical programming experience, the Indian Institute of Embedded Systems (IIES) provides high-quality education and hands-on training in embedded systems. Backed by experienced faculty and an industry-relevant curriculum, IIES is a great choice for students looking to build a successful career in embedded technology.