Shell Advanced Variable operation

Source: Internet
Author: User
Tags reset
first, built-in variables 1. $BASH

The path to the bash's binary program file (the path to the script interpreter)

$ vim test.sh

Enter the code:

#!/bin/bash
Echo $BASH

To run the code:

$ bash test.sh
2. $FUNCNAME

The name of the current function

$ vim test.sh

Enter the code:

#!/bin/bash
xyz23 ()
{
  echo "$FUNCNAME now executing."
  # Normal Print: xyz23 now executing.
  # in individual versions of Bash, $funcname built-in variables are not supported
}

xyz23

echo "FUNCNAME = $FUNCNAME"        # FUNCNAME =
# Exceeding the scope of the function becomes a null value.

To run the code:

$ bash test.sh
3. $IFS

The internal domain delimiter, which is used to determine how Bash recognizes a field or word boundary when interpreting a string.

$IFS defaults to whitespace (spaces, tabs, and line breaks), but this can be modified, for example, when parsing a comma

Data file, you can set it to a comma, noting that $* is using the first character saved in $ifs.

$ vim test.sh

Enter the code:

#!/bin/bash
# $IFS processing whitespace differs from processing other characters.

Output_args_one_per_line ()
{for
  arg does
  echo "[$arg]"
  done
}

echo; echo "ifs=\" \ ""
Echo "-------"

ifs= ""
var= "a  b C   "
output_args_one_per_line $var  # Output_args_one_per_ Line ' echo ' a  b C   "'
# #
[A]
# [b]
# [C]


echo; echo" ifs=: "
echo"-----"

ifs=:
var= ": a::b:c:::"               # as above, but with "" replaced with ":".
Output_args_one_per_line $var # # []
# [
a] # [] # [
b] # [
c] # []
# []
# []

# The same thing can happen in Awk's "FS" domain.

# Thanks, Stephane Chazelas.

Echo

exit 0

Note: You may have questions about the For loop and the variable arg above, which will be discussed in the next section.

To run the code:

$ bash test.sh
4. $REPLY

When no parameter variable is supplied to the Read command, the variable is supplied to the Read command as a default variable. Also

Can be used with the Select menu, but only provide the number of the selected variable, not the value of the variable itself. Built-in variable $reply is not supported in individual versions of Bash.

$ vim test.sh

Enter the code:

#!/bin/bash
# reply.sh

# reply is the default variable provided to the ' read ' command.

echo
echo-n "What's your favorite vegetable?"
Read

echo "Your favorite vegetable is $REPLY."
#
#+ reply saves the value read by the last "read" command when and only if no variable is provided to the "read" command.

echo
echo-n "What's your favorite fruit?"
Read Fruit
echo "Your favorite fruit is $fruit."
echo "But ..."
echo "Value of \ $REPLY is still $REPLY."
#  $REPLY still holds the value of the previous Read command,
#+ because the variable $fruit is passed into the new "read" command.

Echo

exit 0

To run the code:

$ bash test.sh
5. List all the parameters by $* and $@

All positional parameters of "$*" are considered as a single word.

$@ is the same as $*, but each parameter is a separate reference string, which means that the parameter is fully

Passed, and was not interpreted or extended. This also means that each parameter in the argument list is treated as a separate word.

$ vim test.sh

Enter the code:

#!/bin/bash # uses several parameters to invoke this script, such as "one and three". e_badargs=65 if [!-N "$] then echo" Usage: ' basename ' argument1 argument2 etc "Exit $E _badargs fi echo Index

=1 # Start Count.
echo "Listing args with \" \$*\ ":" For Arg in "$*" # If "$*" is not referenced by "" "(double quotes), then it will not work properly.
Do Echo ' Arg # $index = $arg ' let ' index+=1 "done # $* all arguments as one word.

echo "entire arg list seen as single word."

echo index=1 # Reset Count (Translator Note: starting from 1). Echo ' Listing args with \ ' \$@\ ': ' for arg in ' $@ ' do Echo ' arg # $index = $arg ' let ' index+=1 ' done # $@ put each parameter
As a single word.

echo "Arg list seen as separate words."

echo index=1 # Reset Count (Translator Note: starting from 1). echo "Listing args with \$* (unquoted): ' For ARG in $* do Echo ' arg # $index = $arg ' let ' index+=1 ' done # unreferenced
$* will consider the parameters as separate words.

echo "Arg list seen as separate words." Exit 0

To run the code:

$ bash test.sh
second, the operation string 1. String length
$ vim test.sh

Enter the code:

#!/bin/bash

stringz=abcabc123abcabc

echo ${#stringZ}                 #
expr length $stringZ             # 15

To run the code:

$ bash test.sh
2. Using awk to process strings

The bash script can also invoke awk's string manipulation function in place of its own built-in string operation.

Example: Extracting a String

$ vim test.sh

Enter the code:

#!/bin/bash

string=23skidoo1
#      012345678    Bash
#      123456789    awk
# Note the different string indexing system:
# The first character of Bash is recorded from ' 0 '.
# The first character of Awk is recorded starting with ' 1 '.

Echo ${string:2:4} # Position 3 (0-1-2), 4 character
# skid

# The command equivalent to ${string:pos:length in awk is substr (string,pos,length). C13/>echo | awk '
{
    print substr ("'" ${string} "'", 3,4)      # skid
}
'
#  use an empty "echo" Pass the pipeline to awk with a false input,
#+ so you don't have to provide a file name to awk.

Exit 0

To run the code:

$ bash test.sh
Related Article

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.