search
HomeSystem TutorialLINUXHow to distinguish between $() and ${} and $(()) and (()) in bash shell

How to distinguish between $() and ${} and $(()) and (()) in bash shell

Feb 15, 2024 am 10:40 AM
linuxlinux tutorialRed Hatlinux systemlinux commandlinux certificationred hat linuxlinux video

bash shell 中如何区别 $()和${}和$(())和(())

Usage of $() and ${}:

In the bash shell, $( ) and ` ` (backticks) are used for command substitution. And $() can be used in every shell. If you use bash2, there will be no problem...

Look at ${ }... It is actually used for variable substitution. In general, there is no difference between $var and ${var}. But using ${ } will more accurately define the scope of the variable name.

Here I will use some examples to illustrate some of the special functions of ${ }:

Suppose we define a variable as:

file=/dir1/dir2/dir3/my.file.txt

We can use ${ } to replace each other to obtain different values:

${file#*/}: Remove the first / and the string to the left: dir1/dir2/dir3/my.file.txt

${file##*/}: Remove the last / and the string to the left: my.file.txt

${file#*.}: Remove the first . and the string to the left: file.txt

${file##*.}: Remove the last . and the string to the left: txt

${file%/*}: Remove the last / and the string to the right: /dir1/dir2/dir3

${file%%/*}: Remove the first / and the string to the right: (null value)

${file%.*}: Remove the last . and the string to the right: /dir1/dir2/dir3/my.file

${file%%.*}: Remove the first . and the string to the right: /dir1/dir2/dir3/my

The memory method is:

# is to remove the left side (# is to the left of $ on the appraisal plate)

% means removing the right side (% is to the right of $ on the disk)

A single symbol is the minimum match; two symbols is the maximum match.

${file:0:5}: Extract the leftmost 5 bytes: /dir1

${file:5:5}: Extract 5 consecutive bytes to the right of the 5th byte: /dir2

We can also replace the string in the variable value:

${file/dir/path}: Replace the first dir with path:/

path1/dir2/dir3/my.file.txt

${file//dir/path}: Replace all dirs with path:/

path1/path2/path3/my.file.txt

Using ${ } can also assign values ​​to different variable states (unset, null value, non-null value):

${file-my.file.txt}: If $file is not set, use my.file.txt as the return value. (Null and non-null values ​​will not be processed)

${file:-my.file.txt}: If $file is not set or is empty, use my.file.txt as the return value. (Non-null values ​​will not be processed)

${file my.file.txt}: If $file is set to a null value or a non-null value, my.file.txt will be used as the return value. (No processing will be performed if not set)

${file: my.file.txt}: If $file is a non-empty value, use my.file.txt as the return value. (No processing will be performed if there is no setting or empty value)

${file=my.file.txt}: If $file is not set, use my.file.txt as the return value, and assign $file to my.file.txt. (Null and non-null values ​​will not be processed)

${file:=my.file.txt}: If $file is not set or is empty, use my.file.txt as the return value, and assign $file to my.file.txt. (Non-null values ​​will not be processed)

${file?my.file.txt}: If $file is not set, output my.file.txt to STDERR. (Null and non-null values ​​will not be processed)

${file:?my.file.txt}: If $file is not set or is empty, output my.file.txt to STDERR. (Non-null values ​​will not be processed)

The above understanding is that you must clearly distinguish the three assignment states of unset, null and non-null.

Generally speaking, : is related to null. If it does not contain :, null will not be affected. If it contains :, even null will be affected.

Also, ${#var} can calculate the length of the variable value:

${#file} can get 27 because /dir1/dir2/dir3/my.file.txt is exactly 27 bytes...

$(()) Purpose:

It is used for integer operations.

In bash, the integer arithmetic symbols of $(( )) are roughly as follows:

- * /: "Addition, subtraction, multiplication, division" respectively.

%: Remainder operation

& | ^ !: "AND, OR, XOR, NOT" operations respectively.

Example:

wangnc>a=5;b=7;c=2;

wangnc>echo $a $b

5 7

wangnc>echo $(( a b*c))

19

wangnc>echo $(((a*b)/c))

17

wangnc>echo $(($a $b*$c))

19

wangnc>

The variable name in $(( )) can be replaced by adding a $ symbol in front of it, or without it, such as:

$(( $a $b * $c)) can also get the result of 19

In addition, $(( )) can also perform operations with different carry numbers (such as binary, octal, and hexadecimal). However, the output results are all decimal:

echo $((16#2a)) The result is 42 (hexadecimal to decimal)

(())the use of:

In fact, simply using (( )) can also redefine variable values, or do testing:

a=5; ((a )) $a can be redefined as 6

a=5; ((a--)) then a=4

a=5; b=7; ((a

Common test symbols used for (( )) are as follows:

<: less than>: greater than

=: greater than or equal to

==: equal to

!=: Not equal to

The above is the detailed content of How to distinguish between $() and ${} and $(()) and (()) in bash shell. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Linux就该这么学. If there is any infringement, please contact admin@php.cn delete
What are the differences in how Linux and Windows handle device drivers?What are the differences in how Linux and Windows handle device drivers?Apr 25, 2025 am 12:13 AM

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

Compare and contrast the security models of Linux and Windows.Compare and contrast the security models of Linux and Windows.Apr 24, 2025 am 12:03 AM

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor