• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

A view of Special Variables in Shell Scripting

  • 00:39 lsb_release -d
  • 02:09 vim special_var.sh
  • 04:08 sh special_var.sh
  • 04:11 cat special_var.sh
  • 04:23 sh special_var.sh welcome to linuxhelp
6590

A view of Special Variables in Shell Scripting

Introduction:

A shell script is a listing of commands in a computer program that runs by the Unix shell which is a command-line interpreter. The most important special, built-in variables are called positional parameters. These hold the command-line arguments to scripts when they are invoked. Positional parameters have the names 1, 2, 3, etc., meaning that their values are denoted by $1, $2, $3, etc.

Check the OS version by using the below command:

[root@linuxhelp ~]# lsb_release -d
Description:	CentOS Linux release 7.6.1810 (Core) 

Create a special variable file:

[root@linuxhelp ~]# vim special_var.sh

snap1

Execute the special_var script file:

[root@linuxhelp ~]# sh special_var.sh
The output of '' is 
The output of '0' is 0
' and ' output is  and 
the output of '' is 
The output of '0' is 0
The output of '12366' is 12366
The output of '' is 
The output of 'special_var.sh' is your file name = special_var.sh

Show the content of the file:

[root@linuxhelp ~]# cat special_var.sh
#!/bin/bash
echo "The output of '$*' is $*"
echo "The output of '$#' is $#"
echo "'$1 and $2' output is $1 and $2"
echo "the output of '$@' is $@"
echo "The output of '$?' is $?"
echo "The output of '$$' is $$"
echo "The output of '$!' is $!"
echo "The output of '$0' is your file name = $0"

Execute the file with command line arguments:

[root@linuxhelp ~]# sh special_var.sh welcome to linuxhelp
The output of 'welcome to linuxhelp' is welcome to linuxhelp
The output of '3' is 3
'welcome and to' output is welcome and to
the output of 'welcome to linuxhelp' is welcome to linuxhelp
The output of '0' is 0
The output of '12397' is 12397
The output of '' is 
The output of 'special_var.sh' is your file name = special_var.sh

With this method a view of special variables in Shell Scripting comes to an end.

Tags:
albiejames
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What are the special shell variables in the script language?

A

The most important special, built-in variables are called positional parameters. These hold the command-line arguments to scripts when they are invoked. Positional parameters have the names 1, 2, 3, etc., meaning that their values are denoted by $1, $2, $3, etc.

Q

What are $1 and $2 in a shell script?

A

$1 is the first command-line argument passed to the shell script. ... $0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1).

Q

What is $? In shell script?

A

$? -The exit status of the last command executed. $0 -The filename of the current script. $# -The number of arguments supplied to a script. ... For shell scripts, this is the process ID under which they are executing.

Q

How do you write a command-line argument in a shell script?

A

Simply list the arguments on the command line when running a shell script. In the shell script, $0 is the name of the command run (usually the name of the shell script file); $1 is the first argument, $2 is the second argument, $3 is the third argument, etc...

Q

How do I permanently add to my path?

A

To make the change permanent, enter the command PATH=$PATH:/opt/bin into your home directories. bashrc file. When you do this, you're creating a new PATH variable by appending a directory to the current PATH variable, $PATH.

Related Tutorials in A view of Special Variables in Shell Scripting

Related Tutorials in A view of Special Variables in Shell Scripting

How to use Kill, Pkill and Killall to Terminate a Process in Linux
How to use Kill, Pkill and Killall to Terminate a Process in Linux
Jun 6, 2016
How to create and run Bash Script on Rocky Linux 8.6
How to create and run Bash Script on Rocky Linux 8.6
Oct 3, 2022
How to create Shell Script on Debian 12
How to create Shell Script on Debian 12
Sep 29, 2023
How to write a simple shell script and how to use Variables in shell script
How to write a simple shell script and how to use Variables in shell script
Sep 25, 2018
Introduction of Shell Scripting
Introduction of Shell Scripting
Sep 24, 2018
How to create a user by using Shell Script on Oracle Linux 8.5
How to create a user by using Shell Script on Oracle Linux 8.5
Oct 14, 2022
Basic Shell scripting in Linux
Basic Shell scripting in Linux
Apr 13, 2016
A view of Special Variables in Shell Scripting
A view of Special Variables in Shell Scripting
May 8, 2021

Related Forums in A view of Special Variables in Shell Scripting

Related Forums in A view of Special Variables in Shell Scripting

Apache
caden class=
how to use php variables in apache
May 12, 2017
Shell Script
landon class=
How to add multiple user in samba using shell script
Oct 25, 2017
Shell Script
symonds class=
Sort only unique words inside a file
Apr 6, 2018
Shell Script
gibbson class=
How to pass and get variables to user account from root account using shell script
Oct 26, 2017
Environment Variables
caden class=
How to configure environment for emscripten on Ubuntu
Dec 19, 2019
Shell Script
gibbson class=
How to create shell script for ssh login
Oct 10, 2017
Linux
ethan class=
How to get line number of the bash script on run time?
Jun 3, 2024
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.