• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News 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
{{postValue.id}}

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
{{postCtrl.cmtErrMsg}}

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.

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 Sebastian ?
How to change non required to required field in SuiteCRM Custom/Default Modules

How to change not required to the required field in SuiteCRM Custom/Default Modules?

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.