Linux Bash internal variable usage example

Source: Internet
Author: User

$ @


"$ @" Returns all command line parameters as an array. Unlike "$ *", it is returned as a string.
"$ @" Can traverse all elements through loops, as shown in the following script:

#! /Bin/bash
For var in "$ *"; do
Echo $ var
Done
Because $ * only returns a parameter as a string, echo is called only once:

~> $./Testscript. sh firstarg secondarg thirdarg
Firstarg secondarg thirdarg
When using $:

#! /Bin/bash
For var in "$ @"; do
Echo $ var
Done
Return all parameters in an array. You can access each parameter separately:

~> $./Testscript. sh firstarg secondarg thirdarg
Firstarg
Secondarg
Thirdarg
$ #


Obtain the number of command line parameters, and type:

#! /Bin/bash
Echo "$ #"
If the script contains three parameters, the output is as follows:

~> $./Testscript. sh firstarg secondarg thirdarg
3
$!


Returns the ID of the process executed by the previous program:

~> $ Ls &
Testfile1 testfile2
[1] + Done ls
~> $ Echo $!
21715
$


If the pid of the current process is executed under the bash command line, it is equivalent to the pid of the bash process:

~> $ Echo $
13246
$ *


Returns all command line parameters with a single string.
Testscript. sh:

#! /Bin/bash
Echo "$ *"
Run the script with several parameters:

./Testscript. sh firstarg secondarg thirdarg
Output:

Firstarg secondarg thirdarg
$?


Returns the exit status of the upstream function or command execution. 0 indicates that the execution is successful. Otherwise, the execution fails:

~> $ Ls *. blah; echo $?
Ls: cannot access *. blah: No such file or directory
2
~> $ Ls; echo $?
Testfile1 testfile2
0
$1 $2 $3, etc.


The location parameter that is passed to the script or a function from the command line:

#! /Bin/bash
# $ N is the n 'th positional parameter
Echo "$1"
Echo "$2"
Echo "$3"
The output is as follows:
~> $./Testscript. sh firstarg secondarg thirdarg

Firstarg
Secondarg
Thirdarg
If the number of positional parameters is greater than 9, braces must be used:

# "Set --" sets positional parameters
Set -- 1 2 3 4 5 6 7 8 nine ten eleven twelfe
Echo $10 # outputs 1
Echo $ {10} # outputs ten
$ FUNCNAME


Get the name of the current function:

My_function ()
{
Echo "This function is $ FUNCNAME" # This will output "This function is my_function"
}
If this variable is printed out of the function:

My_function
 
Echo "This function is $ FUNCNAME" # This will output "This function is"
$ HOME


User's home directory

~> $ Echo $ HOME
/Home/user
$ IFS


This variable contains the internal field separator used to split the string by bash in a loop. By default, it is a blank character \ n (newline), \ t (tab), or space. If you change it, you can use different separators to split strings:

IFS = ","
INPUTSTR = "a, B, c, d"
For field in $ {INPUTSTR}; do
Echo $ field
Done
Output:
A
B
C
D

$ PWD


Output current working directory

~> $ Echo $ PWD
/Home/user
~> $ Cd directory
Directory> $ echo $ PWD
/Home/user/directory
$ HOSTNAME


Host Name assigned at system startup

~> $ Echo $ HOSTNAME
Mybox.mydomain.com
$ LINENO


The current row number of the output script. It may be used in script debugging.

#! /Bin/bash
# This is line 2
Echo something # this is line 3
Echo $ LINENO # Will output 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.