{"id":2639,"date":"2023-03-25T00:38:50","date_gmt":"2023-03-24T19:08:50","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2639"},"modified":"2024-02-22T21:19:15","modified_gmt":"2024-02-22T15:49:15","slug":"how-to-create-shell-scripts-in-linux-unix","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-create-shell-scripts-in-linux-unix\/","title":{"rendered":"How to Create Shell Scripts in Linux\/Unix"},"content":{"rendered":"\n
In simple terms, shell script in Linux\/Unix is a group of commands you execute in your shell. Just copy and paste your commands into a file and your shell script is prepared. Shell scripts can alleviate your need for many software. Using shell scripts you can automate most of your tasks.<\/strong><\/p>\n\n\n\n In this article, I will talk about Posix shells – mainly bash shell<\/strong>. Most of these commands will also work in other POSIX-compliant shells such as Zsh<\/a> and Dash. First, I will tell you about basic commands like how to set and get variables and their values. Then, I will talk about how to create and run shell scripts. <\/p>\n\n\n\n \ud83d\udcdd Note: <\/strong>If you have knowledge of any programming language, it will be easier to understand bash shell. But I will describe each step, so don’t worry.<\/p>\n\n\n\n Open your terminal, launch the bash shell, and learn these basic commands first.<\/p>\n\n\n\n To define a variable and its value, you just need to follow the simple method:<\/p>\n\n\n\n Where, the value can be an integer, floating point number (i.e. a number with decimal), or strings (i.e. texts)<\/p>\n\n\n\n Examples:<\/p>\n\n\n\n Number:<\/p>\n\n\n\n String:<\/p>\n\n\n\n Please note that there is no space around the equal It is good to always use quotes around your values<\/strong>, otherwise, you might get errors caused by “splitting”, or “substitution”. They are not errors per se. These splitting and substitution have a special application. I will talk about them in later articles.<\/p>\n\n\n\n To get the values of these variables, you need to prepend Example:<\/p>\n\n\n\n Output:<\/p>\n\n\n\n Notice how the If you don’t give any value to any variable, you will get Variable name comprises:<\/p>\n\n\n\n For example, To learn more about bash shell in Linux, look at this article<\/a>.<\/p>\n\n\n\n First, open a file called The first line should tell which shell to be used. For example, to use bash shell, use the following in the first line:<\/p>\n\n\n\n zsh shell<\/strong><\/p>\n\n\n\n dash shell<\/strong>,<\/p>\n\n\n\n Please note that Now, whatever commands you want to run, put them into the file. For examples given above, I will be writing the following commands in the file:<\/p>\n\n\n\n You might already know that files have permissions like read ( Similarly, there is another permission called ‘execute’ permission ( So for the file name Now, your shell script is prepared<\/strong>. You can run it now.<\/p>\n\n\n\n There are many methods to run shell scripts. Two methods are:<\/p>\n\n\n\n Or,<\/p>\n\n\n\n Or,<\/p>\n\n\n\n 2. To run any shell script go to the folder in which it is located on your terminal type For example, to run the above shell script, Now, run the command:<\/p>\n\n\n\n Going to the script’s directory each time we want to run the script is a cumbersome process. To remove this much work, you can include the script’s directory in the variable PATH variable sets a group of directories separated by a colon ( The default value of the So, if you want your computer to find your script, you need to put it in one of these directories. My recommendation will be to create a special directory for your personal scripts. I use Now, move all of your personal script files into this directory. Then add this directory into the PATH using However, the above command is just a temporary addition. When you log out from your system, this path is excluded automatically. To add permanently, open the file Now, you can run your script easily. Just type the file name and hit enter.<\/p>\n\n\n\n For example, if we want to execute the above script Output:<\/p>\n\n\n\n Now, you can bind this script to a shortcut key. Then you will never need a terminal to execute it.<\/p>\n\n\n\n That’s all. This was a basic introduction to shell scripting. Now, you need to learn about Linux command line tools<\/a>:<\/p>\n\n\n\n If you have queries or suggestions, put them in the comment section below.<\/p>\n","protected":false},"excerpt":{"rendered":" It is about how to create and run shell scripts in Linux and Mac. I will also talk about basic commands getting and setting variable values.<\/p>\n","protected":false},"author":2,"featured_media":2664,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[58],"tags":[18,35],"class_list":["post-2639","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","tag-command-line-tools","tag-core-linux-utilities"],"yoast_head":"\nTable of Contents<\/h2>\n\n\n\n
\n
\n
\n
\n
Variables and basic commands in bash shell in Linux<\/h2>\n\n\n\n
How to define variables in bash shell<\/h3>\n\n\n\n
[ajay@legion ~]$ variable=value<\/code><\/pre>\n\n\n\n
[ajay@legion ~]$ x=3<\/code><\/pre>\n\n\n\n
[ajay@legion ~]$ y=\"item_number_one\"<\/code><\/pre>\n\n\n\n
=<\/code> sign.<\/p>\n\n\n\n
[ajay@legion ~]$ variable='United States'<\/code><\/pre>\n\n\n\n
How to get the value of any variable in the bash shell in Linux<\/h3>\n\n\n\n
$<\/code> to these variable names. For example,
x<\/code> becomes
$x<\/code>,
y<\/code> becomes
$y<\/code> and so on. Now, I will be using echo to give you simple examples. To know more about echo, look at my article on it<\/a>.<\/p>\n\n\n\n
[ajay@legion ~]$ echo \"this is value of x: $x\"<\/code><\/pre>\n\n\n\n
this is value of x: 3<\/code><\/pre>\n\n\n\n
$x<\/code> has been substituted by its value.<\/p>\n\n\n\n
null<\/code> result. For example, for an undefined variable
z<\/code>, I get the following result:<\/p>\n\n\n\n
[ajay@legion ~]$ echo \"this is value of z: $z\"\n\nthis is value of z:<\/code><\/pre>\n\n\n\n
\n
\\w<\/code>): numbers, upper and lowercase alphabets, and underscore (
_<\/code> )<\/li>\n\n\n\n
variable_name<\/code>,
variable2<\/code> is allowed but
2variable<\/code> is not allowed.<\/p>\n\n\n\n
How to prepare shell scripts in Linux\/Unix<\/h2>\n\n\n\n
Step 1: Create a file with the first line specifying the shell<\/h3>\n\n\n\n
~\/example.sh<\/code> using your favorite text editor.<\/p>\n\n\n\n
[ajay@legion ~]$ gedit ~\/example.sh<\/code><\/pre>\n\n\n\n
#!\/bin\/bash<\/code><\/pre>\n\n\n\n
#!\/bin\/zsh<\/code><\/pre>\n\n\n\n
#!\/bin\/dash<\/code><\/pre>\n\n\n\n
\/bin\/bash<\/code> is the path to bash binary. You can use any binary like that of python (
\/bin\/python<\/code>), gawk (
\/bin\/gawk<\/code>), etc. If your shell binaries are located in other paths, use them instead. (For example,
#!\/usr\/bin\/bash<\/code>)<\/p>\n\n\n\n
Step 2: Put all shell commands in the file<\/h3>\n\n\n\n
#!\/bin\/bash\nx=3\necho \"this is value of x: $x\"\necho \"this is value of z: $z\"<\/code><\/pre>\n\n\n\n
Step 3: Set the appropriate execution bit on the file<\/h3>\n\n\n\n
r<\/code>) and write (
w<\/code>). If a file does not have write permission, it means you cannot write anything into it.<\/p>\n\n\n\n
x<\/code>). In order to ‘run’ any file, you need to set this permission. Linux, by default, removes this permission on all newly created files. This is a security feature in Linux. So, you need to set the execution bit (i.e. ‘execute’ permission). Use the following command to set the execution bit:<\/p>\n\n\n\n
[ajay@legion ~]$ chmod +x your_file_name<\/code><\/pre>\n\n\n\n
example.sh<\/code>, the command will be:<\/p>\n\n\n\n
[ajay@legion ~]$ chmod +x example.sh<\/code><\/pre>\n\n\n\n
How to run shell scripts in Linux\/Unix<\/h2>\n\n\n\n
\n
[ajay@legion ~]$ bash file_name<\/code><\/pre>\n\n\n\n
[ajay@legion ~]$ sh file_name<\/code><\/pre>\n\n\n\n
[ajay@legion ~]$ zsh file_name<\/code><\/pre>\n\n\n\n
.\/file_name<\/code> and then hit the Enter button.<\/p>\n\n\n\n
~\/example.sh<\/code>. First, open your terminal, go to the home directory using the
cd<\/code> command:<\/p>\n\n\n\n
[ajay@legion ~]$ cd ~ <\/code><\/pre>\n\n\n\n
[ajay@legion ~]$ .\/example.sh<\/code><\/pre>\n\n\n\n
PATH<\/code>.<\/p>\n\n\n\n
PATH variable in Linux<\/h3>\n\n\n\n
:<\/code>). If you run your script from your terminal (or anywhere), the Linux system will look into these directories and try to find the file
example.sh<\/code>. If it cannot find the file, it will throw an error.<\/p>\n\n\n\n
PATH<\/code> variable in your PC can be found using
echo<\/code>:<\/p>\n\n\n\n
[ajay@legion ~]$ echo \"$PATH\"<\/code><\/pre>\n\n\n\n
\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/bin:\/usr\/lib\/jvm\/default\/bin:\/usr\/bin\/site_perl:\/usr\/bin\/vendor_perl:\/usr\/bin\/core_perl:\/home\/ajay\/.local\/share\/npm\/bin<\/code><\/pre>\n\n\n\n
~\/.my_scripts<\/code>:<\/p>\n\n\n\n
[ajay@legion ~]$ mkdir ~\/.my_scripts<\/code><\/pre>\n\n\n\n
export<\/code>:<\/p>\n\n\n\n
export PATH=${PATH}:~\/.my_scripts\/<\/code><\/pre>\n\n\n\n
~\/.profile<\/code> and\/or
~\/.zshrc<\/code> using your favorite text editor and add the above
export<\/code> line in the end.<\/p>\n\n\n\n
[ajay@legion ~]$ file_name<\/code><\/pre>\n\n\n\n
example.sh<\/code>:<\/p>\n\n\n\n
[ajay@legion ~]$ example.sh<\/code><\/pre>\n\n\n\n
this is value of x: 3\nthis is value of z: <\/code><\/pre>\n\n\n\n
Conclusion<\/h2>\n\n\n\n
\n