The book “Learning the bash Shell” 3rd ed. by Cameron Newham and Bill Rosenblatt has at page 181 a picture describing the command-line processing of bash shell. That picture shows that Command substitution is performed before Arithmetic substitution (sic). The bash man page, though, reports a different order: “The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.” Is the book wrong? If it is, can you please provide an example demonstrating it?
Order of Command substitution and Arithmetic expansion in bash
115 views Asked by diciotto At
2
There are 2 answers
1
Socowi
On
The official documentation is right (but mind the subtle difference between "," and ";" in there):
Command substitution $()/`` and arithmetic substitution $(()) have the same precedence. Whatever comes first when reading left to right, is expanded first.
You can confirm this with a test. Here, we use the built-in variable $SECONDS, which contains the time in seconds since bash started.
#! /bin/bash
echo "$(sleep 1; echo "command $SECONDS"; sleep 1), arithmetic $((SECONDS))"
echo "arithmetic $((SECONDS)), $(sleep 1; echo "command $SECONDS"; sleep 1)"
This prints
command 1, arithmetic 2
arithmetic 2, command 3
Related Questions in BASH
- When does Bash read heredocs?
- Why `set -o pipefail` gives different output even though the pipe is not failing
- Run an external command within jq to manipulate each values of a particular key
- API key 401 error in .env.development file
- How to "Enable mobile data" on a Huawei E3372 4G USB dongle using a bash script in Windows
- ImageMagick / Bash : pipe ignored(?) when filename format variable used
- MacOS Bash-Script: while read p and echo
- Parse command line arguments and write useful usage message without additional code
- JQ JSON - Values to Array
- why variable substitution is so different?
- postbank_pdf2csv: how to setup with Cygwin in Windows?
- Custom Bash functions & custom statements - Need some advice
- unexpected operator == in square brackets when trying to use gum lib
- How to disable a bash builtin inside a docker container
- Use sed or rename find series of alphabet then replace with with the same alphabet and a dash -
Related Questions in COMMAND-LINE
- ZSH function parameters conundrum
- Parse command line arguments and write useful usage message without additional code
- Playwright JS: Getting an error when debugging using line numbers
- NPM Command Line Tool - Command not working
- How to get argument from Command line for my python package
- Send a json command to an app at a known port/address?
- Using command to open Privacy & Security > Proflies page
- Extract filepaths of .MP4 files in subfolders of main folder to textfile
- Does Mac OS have different Git versioning?
- Trouble Fetching Go Modules: Network Timeout When Accessing proxy.golang.org
- Why does command substitution work in these ways?
- Finding total RAM consumption of process, including swap
- When using the ripgrep command in the Terminal how can I search for files which contain one pattern AND another pattern?
- I can't use command "start" in git bash
- Executing a Jupyter Notebook from the Command Line with a Specified Kernel
Related Questions in SUBSTITUTION
- Substituting values in a Python sympy unknown function
- A multi-line substitution in RST
- Getting error 'Some bytes have been replaced with the Unicode substitution character while loading file ... with Unicode (UTF-8)"
- Extracting text after nth of a character and put in next row , under first row in Excel VBA Code
- Parsing data with Pandas - how to output match as a new column
- Substitution AND capitalize on ${var}
- CS50 - C -problemset 2 - substitution - output not valid ASCII text
- Is there a way to use the substitution function in VBA to change multiple values in a single column into a different text output?
- What is causing the inefficiency of GNU sed using `.*` substitutions?
- Google Sheets - replace the exact text/number with a different one ignoring other matching parts
- Variable substitution inside if statement
- Problem with Powershell variable substitution in Strings
- Substitue values in jq from a txt file
- Modifying a global variable works, but not when a pipe is used
- Turn off infuriating Xcode editor completions
Related Questions in EXPANSION
- fzf bash refresh / re expannd and substitute $() and `` var expansion with binding in --reload() option
- Is there any PyPSA sample of the Energy Storage capacity expansion model?
- Preserving Spaces in Array Elements with Mixed Strings and Command-Line Flags
- How to expand a Boolean expression in the C++ preprocessor?
- Bash, Fish: prevent variable expansion
- flutter: ExpansionTile with controller error in ListView
- Changing title of ExpansionTile, when expanded in flutter
- Snakemake: wildcard restriction
- Horizontal Expansion Widgets
- Order of Command substitution and Arithmetic expansion in bash
- C preprocessor stringify macro without expanding its components
- Creating a list of possibilities from splitting strings at a certain character - Karnaugh Map
- The difference of compression mechanisms between OLAP and TSDB databases
- Solved: How do i loop through arrays using variable expansion on strings in batch?
- Apache-beam Expansion service failed to build transform for Python ReadFromJdbc #apache_beam.io.jdbc
Related Questions in COMMAND-SUBSTITUTION
- Blank variable during bash command substitution
- Bash: Insert variable content into command substitution as arguments - how to quote correctly?
- run command inside of ${ curly braces
- How can I call a Bash function without invoking command substitution?
- Bash: Checking for exit status of multi-pipe command chain
- command substitution in docker CMD
- variable error in bash when doing calculation
- Does awk -v accept command substitution?
- command substitution not working in alias?
- Quote handling in variables inside a command substitution
- Get git commit SHA for use in command line utility
- bash filename as parameter in jq commands
- Bash command substitution within a function does not return the return code from the command substitution
- Use substituted string as a command in shell script
- Command substitution as a variable in one-liner
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
1st of all, according to the official bash documentation The order is as follows:
As @socowi points out in his answer, there is a difference between
;and,when it comes to the order of operations, so kinds 2 - 5 are done simultaneously, left to right.So as you've suspected your book is wrong. Now, I initially suspected that if command substitution would happen after arithmetic expansion we could wind up with situations like:
This was wrong. As
(echo "if command substitution happens before arithmetic substitution, this $((2+3)) is 5 else it's 2+3")is a command on its own that means that the expansion order occurs within it too so that situation is impossible.In fact if you run
set -vxbefore the above experiment you can see that, as the innermost expansion,$((2+3))is performed immediately.