Shell Programming: Bash Space Things
First understand when to use spaces in bash and when not to use them.
1. There cannot be spaces on both sides of the equal sign assignment
2. Spaces are required between commands and options
3. Spaces on both sides of the pipe are optional
Let’s take a look at common problems
1. Equal signs when assigning values There are extra spaces on both sides or only on the left
test@pythontab.com ~ $ var1 = test
bash: var1: command not found
test@pythontab.com ~ $ echo ${var1:?error}
bash: var1: error
test @pythontab.com ~ $ echo ${var1?error}
bash: var1: error
test@pythontab.com ~ $ var2 =test
bash: var2: command not found
test@pythontab.com ~ $ echo ${var2 :?error}
bash: var2: error
test@pythontab.com ~ $ echo ${var2?error}
bash: var2: error
Here I used bash variable expansion, ${var1:?error} when When var1 is unset or null (undefined or empty), the specified error is reported; ${var1?error}When var1 is unset, the specified error is reported. Judging from the execution results, if there is a space on the left side of the equal sign, the variable name is executed as a command, and the result is command not found, and the variable is not assigned. 2. When assigning a value, there is no space on the left side of the equal sign, but there is a space on the right side (this situation is a bit In particular, you will find two situations)
test@pythontab.com ~ $ var= test
test@pythontab.com ~ $ var= nocmd
bash: nocmd: command not found
There is also a space on the right side of the equal sign. One command did not report an error, but the second one did.
This is because there is such a way to execute commands in the shell: var=string command
The command command will get the value of the variable var (as for whether the value of the variable var is retained after the command is executed, it is not retained in bash4, But I kept it when I found it in dash. Different shells handle this differently). Since test is a command and nocmd is not, I reported command not found.
test@pythontab.com ~ $ var=newtest eval echo $var
newtest
test@pythontab.com ~ $ echo $var
Note: I used eval here to avoid $var being replaced with an empty string during the first parsing, otherwise the following situation will occur (The following is the wrong test method. Before echo is executed, $var has been replaced with an empty string)
The code is as follows:
test@pythontab.com ~ $ var=newtest echo $var
test@pythontab.com ~ $ echo $var
At this point, I believe everyone understands that for equal sign assignment, there cannot be spaces on the left and right sides. Although spaces on the right may not necessarily cause an error, that is definitely not the result you want.
3. There must be a space between the command and the option
Everyone seems to understand this, why am I still so wordy? Speaking of which, I have to mention a very special command: [ command (you read that right, it is [ ), which is the test command (of course in bash, this is a built-in command, but it does not affect our understanding here) ). Maybe you will think that the [command looks familiar, yes, I guarantee you have seen it, take a look at the following example
test@pythontab.com ~ $ if [ "abc" = "abc" ]; then echo 'they are the same '; fi
they are the same
test@pythontab.com ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/[
Remember? [Commands are often used in if judgments. Of course, some people like to write them like this
test@pythontab.com ~ $ [ "abc" = "cba" ] || echo 'they are not the same'
they are not the same
test @pythontab.com ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/[
[ The command is called test command. They are almost the same. Why are they not exactly the same? Let’s take a look at this
test@pythontab.com ~ $ [ "abc" = "cba"
bash: [: missing `]'
test@pythontab.com ~ $ [ "abc" = "cba" ]
test@pythontab .com ~ $ test "abc" = "cba" ]
bash: test: too many arguments
test@pythontab.com ~ $ test "abc" = "cba"
It’s clear, when using the [command, you must Give it a tail], when using the test command, you cannot add a tail. Tail] is [the last parameter, an indispensable parameter, which represents the end of the command
I have said so much, but what does this have to do with spaces? Let me say this first to let everyone understand: [It is a command in the shell, and there must be spaces around it! ] is the last indispensable parameter of [, which also requires spaces on both sides (although the parameters of some commands can be connected together, such as ps, the [ command cannot, there must be spaces between its parameters). Let's look at [common mistakes
a. Missing space between if and [
test@pythontab.com ~ $ if[ "$HOME" = "/home/igi"]; then echo 'equal'; fi
bash: syntax error near unexpected token `then'
test@pythontab.com ~ $ if[ "$HOME" = "/home/igi" ];then echo 'equal'; fi
bash: syntax error near unexpected token ` then'
test@pythontab.com ~ $ if["$HOME" = "/home/igi"];then echo 'equal'; fi
bash: syntax error near unexpected token `then'
test@pythontab.com ~ $ if["$HOME" = "/home/igi" ];then echo 'equal'; fi
bash: syntax error near unexpected token `then'
Syntax analysis error, obviously, if[ For bash, I don’t know what the hell it is
b. There is a lack of space between [ and the following parameters
test@pythontab.com ~ $ if ["$HOME" = "/ home/igi" ];then echo 'equal'; fi
bash: [/home/igi: No such file or directory
test@pythontab.com ~ $ if ["$HOME" = "/home/igi"]; then echo 'equal'; fi
bash: [/home/igi: No such file or directory
["$HOME" For bash, I don’t know what the hell it is
c. [ ] The parameters between Missing space between
test@pythontab.com ~ $ if [ "abc"="abc" ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if [ "abc"="cba" ] ; then echo 'equal'; fi
equal
The first command seems to be correct (actually it is just a coincidence), look at the second command "abc" and "cba" are obviously different, but they are judged to be the same. This is because there is a lack of space between the parameters, and the [command considers the internal value to be just a value. Take a look at the following command and you will be relieved
test@pythontab.com ~ $ if [ 0 ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if [ "1" ]; then echo ' equal'; fi
equal
test@pythontab.com ~ $ if [ "" ]; then echo 'equal'; fi
test@pythontab.com ~ $ if [ ]; then echo 'equal'; fi
in [ ] Internally, true if there is only one value (those concatenated by missing spaces are also counted) that is not the empty string. Therefore, the parameters between [ ] should also have spaces on both sides, and cannot be stacked together
d. There is a lack of space between parameters and tail]
This is not verbose, tail] is also a parameter of [command, as above As mentioned, there must be spaces between parameters
So much about [commands and spaces], but sometimes, it can run correctly without spaces. Of course, this is just your good luck. Let’s take a look
test@pythontab. com ~ $ var=' abc'
test@pythontab.com ~ $ if [$var = "abc" ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if ["$var" = "abc" ];then echo 'equal'; fi
bash: [ abc: command not found
Double quotation marks surround a whole, and when there are no double quotation marks, the spaces or tabs before and after the string are Cut. If you happen to encounter it or you deliberately want to discard spaces or tabs before and after the string, it is not impossible, but it is highly not recommended that you write this, your code will be very fragile.
Or you have added all the spaces you should add, but still get an error. This may also be related to the lack of double quotes. This situation is very common, take a look at it finally
test@pythontab.com ~ $ var=''
test@pythontab.com ~ $ if [ "$var" = "abc" ];then echo 'equal'; fi
test@pythontab.com ~ $ if [ $var = "abc" ];then echo 'equal'; fi
bash: [: =: unary operator expected
test@pythontab.com ~ $ dvar='a b c'
test @pythontab.com ~ $ if [ $dvar = "a b c" ];then echo 'equal'; fi
bash: [: too many arguments
test@pythontab.com ~ $ if [ "$dvar" = "a b c" ] ;then echo 'equal'; fi
equal
I'll be verbose again, don't omit the double quotes easily. Is it clear? If you still don’t understand,
Finally, I won’t talk nonsense about the optional spaces on both sides of the pipe, because I haven’t met anyone who has any doubts about this.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



<p>Customizing your operating system is a great way to make your daily life more enjoyable. You can change the user interface, apply custom themes, add widgets, and more. So today we will show you how to install ClassicShell on Windows 11. </p><p>This program has been around for a long time and allows you to modify the operating system. Volunteers have now started running the organization, which disbanded in 2017. The new project is called OpenShell and is currently available on Github for those interested. </p>&a

Do you see this error message "Add-AppxPackage: Deployment failed with HRESULT: 0x80073D02, The package cannot be installed because the resource it modifies is currently in use. Error 0x80073D02..." in PowerShell when you run the script? As the error message states, this does occur when the user attempts to re-register one or all WindowsShellExperienceHost applications while the previous process is running. We've got some simple solutions to fix this problem quickly. Fix 1 – Terminate the experience host process You must terminate before executing the powershell command
![Explorer.exe does not start on system startup [Fix]](https://img.php.cn/upload/article/000/887/227/168575230155539.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Nowadays, many Windows users start encountering severe Windows system problems. The problem is that Explorer.exe cannot start after the system is loaded, and users cannot open files or folders. Although, Windows users can open Windows Explorer manually using Command Prompt in some cases and this must be done every time the system restarts or after system startup. This can be problematic and is due to the following factors mentioned below. Corrupted system files. Enable fast startup settings. Outdated or problematic display drivers. Changes were made to some services in the system. Modified registry file. Keeping all the above factors in mind, we have come up with some that will surely help the users

When processing files under Linux systems, it is sometimes necessary to delete lines at the end of the file. This operation is very common in practical applications and can be achieved through some simple commands. This article will introduce the steps to quickly delete the line at the end of the file in Linux system, and provide specific code examples. Step 1: Check the last line of the file. Before performing the deletion operation, you first need to confirm which line is the last line of the file. You can use the tail command to view the last line of the file. The specific command is as follows: tail-n1filena

Open shell not running on Windows 11 is not a new problem and has been plaguing users since the advent of this new operating system. The cause of the Open-Shell Windows 11 not working issue is not specific. It can be caused by unexpected errors in programs, the presence of viruses or malware, or corrupted system files. For those who don’t know, Open-Shell is the replacement for Classic Shell, which was discontinued in 2017. You can check out our tutorial on how to install Classic Shell on Windows 11. How to replace Windows 11 Start menu

Windows Subsystem for Linux The first option is to use Windows Subsystem for Linux or WSL, which is a compatibility layer for running Linux binary executables natively on Windows systems. It works for most scenarios and allows you to run shell scripts in Windows 11/10. WSL is not automatically available, so you must enable it through your Windows device's developer settings. You can do this by going to Settings > Update & Security > For Developers. Switch to developer mode and confirm the prompt by selecting Yes. Next, look for W

Some examples of Python scripts: enterprise WeChat alarms, FTP clients, SSH clients, Saltstack clients, vCenter clients, obtaining domain name SSL certificate expiration time, sending today's weather forecast and future weather trend charts; some examples of Shell scripts: SVN Full backup, Zabbix monitoring user password expiration, building local YUM, and the readers' needs in the previous article (when the load is high, find out the process scripts with high occupancy and store or push notifications); it is a bit long, so please read it patiently At the end of the article, there is an Easter egg after all. Python script part of enterprise WeChat alarm This script uses enterprise WeChat application to perform WeChat alarm and can be used

OpenShell is a free software utility that can be used to customize the Windows 11 Start menu to resemble a classic-style menu or a Windows 7-style menu. The Start menu on previous versions of Windows provided users with an easy way to browse the contents of their system. Basically, OpenShell is a replacement for ClassicShell that provides different user interface elements that help to get the functionality of the latter version from previous Windows versions. Once development of ClassicShell ceased in 2017, it was maintained and developed by GitHub volunteers under the name OpenShell. It is related to Win
