Bash Command Substitution

Command substitution and process management

Command Substitution

$(command) # execute command and capture output
`command` # old-style (not recommended)

# Example:
files=$(ls)
date=$(date +%Y-%m-%d)

Arithmetic Operations

$((expression)) # arithmetic evaluation
$((5 + 3)) # returns 8
$((x * 2)) # multiply by 2
((x++)) # increment
((x--)) # decrement

Process Substitution

<(command) # treat output as file
>(command) # treat input as file

# Example:
diff <(ls dir1) <(ls dir2)

Background Jobs

command & # run in background
jobs # list background jobs
fg # bring job to foreground
bg # resume job in background
wait # wait for background jobs

Command Chaining

command1 ; command2 # run sequentially
command1 && command2 # run command2 if command1 succeeds
command1 || command2 # run command2 if command1 fails