[Question]
  [
What general tips do you have for golfing in Bash? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Bash (e.g. "remove comments" is not an answer). Please post one tip per answer.
  
[Answer]
***Undocumented***, but works in every version I've run into for legacy `sh` backwards compatibility:
`for` loops allow you to use `{` `}` instead of `do` `done`. E.g. replace:
```
for i in {1..10};do echo $i; done
```
with:
```
for i in {1..10};{ echo $i;}
```
[Answer]
For arithmetic expansion use `$[…]` instead of `$((…))`:
```
bash-4.1$ echo $((1+2*3))
7
bash-4.1$ echo $[1+2*3]
7
```
In arithmetic expansions don't use `$`:
```
bash-4.1$ a=1 b=2 c=3
bash-4.1$ echo $[$a+$b*$c]
7
bash-4.1$ echo $[a+b*c]
7
```
Arithmetic expansion is performed on indexed array subscripts, so don't use `$` neither there:
```
bash-4.1$ a=(1 2 3) b=2 c=3
bash-4.1$ echo ${a[$c-$b]}
2
bash-4.1$ echo ${a[c-b]}
2
```
In arithmetic expansions don't use `${…}`:
```
bash-4.1$ a=(1 2 3)
bash-4.1$ echo $[${a[0]}+${a[1]}*${a[2]}]
7
bash-4.1$ echo $[a[0]+a[1]*a[2]]
7
```
[Answer]
The normal, lengthy and boring way to define a function is
```
f(){ CODE;}
```
As [this guy](//stackoverflow.com/q/21186724 "Why is whitespace sometimes needed around metacharacters?") found out, you absolutely need the space before `CODE` and the semicolon after it.
This is a little trick I've learned from [@DigitalTrauma](/users/11259):
```
f()(CODE)
```
That is two characters shorter and it works just as well, provided that you don't need to carry over any changes in variables' values after the function returns ([the parentheses run the body in a subshell](https://codegolf.stackexchange.com/a/20939)).
As [@jimmy23013](https://codegolf.stackexchange.com/users/25180/user23013) points out in the comments, even the parentheses may be unnecessary.
The [Bash Reference Manual](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Functions "Bash Reference Manual # 3.3 Shell Functions") shows that functions can be defined as follows:
> 
> 
> ```
> name () compound-command [ redirections ]
> 
> ```
> 
> or
> 
> 
> 
> ```
> function name [()] compound-command [ redirections ]
> 
> ```
> 
> 
A [compound command](http://www.gnu.org/software/bash/manual/bashref.html#Compound-Commands "Bash Reference Manual # 3.2.4 Compound Commands") can be:
* a Looping Construct: `until`, `while` or `for`
* a Conditional Construct: `if`, `case`, `((...))` or `[[...]]`
* Grouped Commands: `(...)` or `{...}`
That means all of the following are valid:
```
$ f()if $1;then $2;fi
$ f()($1&&$2)
$ f()(($1))                # This one lets you assign integer values
```
And I've been using curly brackets like a sucker...
[Answer]
`:` is a command that does nothing, its exit status always succeeds, so it can be used instead of `true`.
[Answer]
More tips
1. Abuse the ternary operator, `((test)) && cmd1 || cmd2` or `[ test ] && cmd1 || cmd2`, as much as possible.
Examples (length counts always exclude the top line):
```
    t="$something"
    if [ $t == "hi" ];then
    cmd1
    cmd2
    elif [ $t == "bye" ];then
    cmd3
    cmd4
    else
    cmd5
    if [ $t == "sup" ];then
    cmd6
    fi
    fi
```
By using ternary operators only, this can easily be shortened to:
```
    t="$something"
    [ $t == "hi" ]&&{
    cmd1;cmd2
    }||[ $t == "bye" ]&&{
    cmd3;cmd4
    }||{
    cmd5
    [ $t == "sup" ]&&cmd6
    }
```
As nyuszika7h pointed out in the comments, this specific example could be shortened even further using `case`:
```
    t="$something"
    case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac
```
1. Also, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e. `{` and `}`) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:
```
 t=$something
 [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
```
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using `-eq` for integers).
1. One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
```
 t=$something
 [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)
```
2. In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension - see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
```
 [[ $f == b ]]&&: # ... <-- Bad
 [ $f == b ]&&: # ... <-- Better
 [ $f = b ]&&: # ... <-- Best.  word splits and pathname-expands the contents of $f.  Esp. bad if it starts with -
```
Note this caveat, especially in checking for null output or an undefined variable:
```
    [[ $f ]]&&:    # double quotes aren't needed inside [[, which can save chars
    [ "$f" = '' ]&&: <-- This is significantly longer
    [ -n "$f" ]&&:
```
In all technicality, this specific example would be best with `case` ... `in`:
```
    t=$something
    case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac
```
So, the moral of this post is this:
1. Abuse the boolean operators as much as possible, and **always** use them instead of `if`/`if-else`/etc. constructs.
2. Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
3. Avoid quotes as much as physically possible.
4. Check out `case` ... `in`, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
```
< > ( ) ; & | <space> <tab>
```
---
EDIT: As manatwork pointed out, the double parenthesis test only works for integers. Also, indirectly, I found that you need to have whitespace surrounding the `==` operator. Corrected my post above.
I also was too lazy to recalculate the length of each segment, so I simply removed them. It should be easy enough to find a string length calculator online if necessary.
[Answer]
Avoid `$( ...command... )`, there is an alternative which saves one char and does the same thing:
```
` ...command... `
```
[Answer]
If you need to pass the content of a variable to STDIN of the next process in a pipeline, it is common to echo the variable into a pipeline. But you can achieve the same thing with a `<<<` [bash here string](http://www.gnu.org/software/bash/manual/bashref.html#Here-Strings):
```
$ s="code golf"
$ echo "$s"|cut -b4-6
e g
$ cut -b4-6<<<"$s"
e g
$ 
```
[Answer]
# One-line `for` loops
An arithmetic expression concatenated with a range expansion will be evaluated for each item in the range. For example the following:
```
: $[expression]{0..9}
```
will evaluate `expression` 10 times.
This is often significantly shorter than the equivalent `for` loop:
```
for((;10>n++;expression with n)){ :;}
: $[expression with ++n]{0..9}
```
If you don't mind command not found errors, you can remove the inital `:`. For iterations larger than 10, you can also use character ranges, for example `{A..z}` will iterate 58 times.
As a practical example, the following both produce the first 50 triangular numbers, each on their own line:
```
for((;50>d++;)){ echo $[n+=d];} # 31 bytes
printf %d\\n $[n+=++d]{A..r}    # 28 bytes
```
[Answer]
Instead of `grep -E`, `grep -F`, `grep -r`, use `egrep`, `fgrep`, `rgrep`, saving two chars. The shorter ones are deprecated but work fine.
(You did ask for one tip per answer!)
[Answer]
Element 0 of an array may be accessed with the variable name only, a five byte saving over explicitly specifying an index of 0:
```
$ a=(code golf)
$ echo ${a[0]}
code
$ echo $a
code
$ 
```
[Answer]
### Use arithmetic `(( ... ))` for conditions
You could replace:
```
if [ $i -gt 5 ] ; then
    echo Do something with i greater than 5
fi
```
by
```
if((i>5));then
    echo Do something with i greater than 5
fi
```
(Note: There is no space after `if`)
or even
```
((i>5))&&{
    echo Do something with i greater than 5
}
```
... or if only one command
```
((i>5))&&echo Echo or do something with i greater than 5
```
### Further: Hide variable setting in arithmetic construct:
```
((i>5?c=1:0))&&echo Nothing relevant there...
# ...
((c))&&echo Doing something else if i was greater than 5
```
or same
```
((c=i>5?c=0,1:0))&&echo Nothing relevant there...
# ...
((c))&&echo Doing something else if i was greater than 5
```
... where *if i > 5, then c = 1* (not 0;)
[Answer]
A shorter syntax for infinite loops (which can be escaped with `break` or `exit` statements) is
```
for((;;)){ code;}
```
This is shorter than `while true;` and `while :;`.
If you don't need `break` (with `exit` as the only way to escape), you can use a recursive function instead.
```
f(){ code;f;};f
```
If you do need break, but you don't need exit and you don't need to carry over any variable modification outside the loop, you can use a recursive function [with parentheses around the body](https://codegolf.stackexchange.com/a/20939), which run the function body in a subshell.
```
f()(code;f);f
```
[Answer]
# Loop over arguments
As noted in [Bash “for” loop without a “in foo bar…” part](https://unix.stackexchange.com/q/417292/11630), the `in "$@;"` in `for x in "$@;"` is redundant.
From [`help for`](https://tio.run/##S0oszvj/PyM1p0AhLb/o/38A "Bash – Try It Online"):
```
for: for NAME [in WORDS ... ] ; do COMMANDS; done
    Execute commands for each member in a list.
    The `for' loop executes a sequence of commands for each member in a
    list of items.  If `in WORDS ...;' is not present, then `in "$@"' is
    assumed.  For each element in WORDS, NAME is set to that element, and
    the COMMANDS are executed.
    Exit Status:
    Returns the status of the last command executed.
```
For example, if we want to square all numbers given positional arguments to a Bash script or a function, we can do this.
```
for n;{ echo $[n*n];}
```
[Try it online!](https://tio.run/##S0oszvj/Py2/SCHPulohNTkjX0ElOk8rL9a69v///0ZG/42Njf@bAAEA "Bash – Try It Online")
[Answer]
# Use `if` to group commands
Compared to [this tip](https://codegolf.stackexchange.com/a/20939/25180) which removes the `if` at all, this should only work better in some very rare cases, such as when you need the return values from the `if`.
If you have a command group which ends with a `if`, like these:
```
a&&{ b;if c;then d;else e;fi;}
a&&(b;if c;then d;else e;fi)
```
You can wrap the commands before `if` in the condition instead:
```
a&&if b;c;then d;else e;fi
```
Or if your function ends with a `if`:
```
f(){ a;if b;then c;else d;fi;}
```
You can remove the braces:
```
f()if a;b;then c;else d;fi
```
[Answer]
# Use `[` instead of `[[` and `test` when possible
**Example:**
```
[ -n $x ]
```
  
# Use `=` instead of `==` for comparison
**Example:**
```
[ $x = y ]
```
Note that you **must** have spaces around the equals sign or else it won't work. Same applies to `==` based on my tests.
[Answer]
# Alternative to cat
Say you are trying to read a file and use it in something else. What you might do is:
```
echo foo `cat bar`
```
If the contents of `bar` was `foobar`, this would print `foo foobar`.
However, there is an alternative if you are using this method, which saves 3 bytes:
```
echo foo `<bar`
```
[Answer]
Sometimes it is shorter to use the `expr` builtin for displaying the result of a simple arithmetic expression instead of the usual `echo $[ ]`. For example:
```
expr $1 % 2
```
is one byte shorter than:
```
echo $[$1%2]
```
[Answer]
# Shorten file names
In a recent challenge I was trying to read the file `/sys/class/power_supply/BAT1/capacity`, however this can be shortened to `/*/*/*/*/capac*y` as no other file exists with that format.
For example, if you had a directory `foo/` containing the files `foo, bar, foobar, barfoo` and you wanted to reference the file `foo/barfoo`, you can use `foo/barf*` to save a byte.
The `*` represents "anything", and is equivalent to the regex `.*`.
[Answer]
## `tr -cd` is shorter than `grep -o`
For example, if you need to count spaces, `grep -o <char>` (print only the matched) gives 10 bytes while `tr -cd <char>` (delete complement of `<char>`) gives 9.
```
# 16 bytes
grep -o \ |wc -l
# 15 bytes
tr -cd \ |wc -c
```
([source](https://codegolf.stackexchange.com/a/167212/41805 "link to answer where tr is better than grep"))
Note that they both give slightly different outputs. `grep -o` returns line separated results while `tr -cd` gives them all on the same line, so `tr` might not always be favourable.
[Answer]
Use tail recursion to make loops shorter:
These are equivalent in behavior (though probably not in memory/PID usage):
```
while :;do body; done
f()(body;f);f
body;exec $0
body;$0
```
And these are roughly equivalent:
```
while condition; do body; done
f()(body;condition&&f);f
body;condition&&exec $0
body;condition&&$0
```
(technically the last three will always execute the body at least once)
Using `$0` requires your script to be in a file, not pasted into the bash prompt.
Eventually your stack might overflow, but you save some bytes.
[Answer]
# Alternatives to `head`
`line` is three bytes shorter than `head -1`, but is being [deprecated](https://git.kernel.org/cgit/utils/util-linux/util-linux.git/tree/Documentation/deprecated.txt#n57).
`sed q` is two bytes shorter than `head -1`.
`sed 9q` is one byte shorter than `head -9`.
[Answer]
Use a pipe to the `:` command instead of `/dev/null`. The `:` built-in will eat all its input.
[Answer]
`split` has another (deprecated, but nobody cares) syntax for splitting input into sections of `N` lines each: instead of `split -lN` you can use `split -N` e.g. `split -9`.
[Answer]
### Expand away the tests
Essentially, the shell is a kind of macro language, or at least a hybrid or some kind. Every command-line can be basically broken into two parts: the parsing/input part and the expansion/output part.
The first part is what most people focus on because it's the most simple: you see what you get. The second part is what many avoid ever even trying to understand very well and is why people say things like *`eval` is evil* and *always quote your expansions* - people want the result of the first part to equal the first. That's ok - but it leads to unnecessarily long code branches and tons of extraneous testing.
Expansions are *self-testing*. The *`${param[[:]#%+-=?]word}`* forms are more than enough to validate the contents of a parameter, are nestable, and are all based around evaluating for *NUL* - which is what most people expect of tests anyway. `+` can be especially handy in loops:
```
r()while IFS= read -r r&&"${r:+set}" -- "$@" "${r:=$*}";do :;done 2>&-
IFS=x
printf %s\\n some lines\ of input here '' some more|{ r;echo "$r"; }
```
---
```
somexlines ofxinputxhere
```
...while `read` pulls in not blank lines `"${r:+set}"` expands to `"set"` and the positionals get `$r` appended. But when a blank line is `read`, `$r` is empty and `"${r:+set}"` expands to `""` - which is an invalid command. But because the command-line is expanded *before* the `""` null command is searched, `"${r:=$*}"` takes the values of all of the positionals concatenated on the first byte in `$IFS` as well. `r()` could be called again in `|{` compound command `;}` w/ a different value for `$IFS` to get the next input paragraph as well, since it is illegal for a shell's `read` to buffer beyond the next `\n`ewline in input.
[Answer]
# Use `pwd` instead of `echo` to generate a line of output
Need to put a line on stdout but don't care about the contents, and want to restrict your answer to shell builtins? `pwd` is a byte shorter than `echo`.
[Answer]
Quotes can be omitted when printing strings.
```
echo "example"
echo example
```
Output in SM-T335 LTE, Android 5.1.1:
```
u0_a177@milletlte:/ $ echo "example"
example
u0_a177@milletlte:/ $ echo example
example
```
[Answer]
# Abuse function definitions to check for equality
If you want to check if two strings are equal to each other, the obvious way is to use the `[[`/`test`/`[` builtins:
```
[[ $a == b ]]
[[ $a = b ]]
[ $a = b ]
```
But in fact often a shorter way is to define a do-nothing function under the name of `b` and then try to call it with `$a`.
```
b()(:);$a
# zsh only:
b():;$a
```
Depending on the possible values `$a` can take, you might need to add some characters to ensure that it never coincides with the name of a real command:
```
,b()(:);,$a
# zsh only:
,b();,$a
```
If you're testing and immediately using `&&`, then you can put generally put the command after the `&&` as the body of the function:
```
[ $a = b ]&&foo
b()(foo);$a
# zsh only:
b()foo;$a
```
Here are a few answers I've abused this in (Zsh not Bash, but the same idea applies):
* <https://codegolf.stackexchange.com/a/219018>
* <https://codegolf.stackexchange.com/a/219167>
[Answer]
#### Output a string based on a test string or numeric test with `expr`
From [my question](https://codegolf.stackexchange.com/q/258174/70305):
For any form of test [`expr`](https://www.man7.org/linux/man-pages/man1/expr.1.html) supports (numerical or string equality, inequality, less than, greater than, regex pattern match, with support for math), there are often ways to use it to improve on the naive approach to echoing a particular string based on a given test. For example, the most straightforward approach to performing an "is a program argument a substring of a fixed string?" test, echoing the string `true` or `false` based on the result, assuming the substring contains no regex special characters, is:
```
[[ FIXEDSTRING =~ $1 ]]&&echo true||echo false
```
But with `expr`, we can shave off six characters by careful use of `&` and `|` to select the string that results from the overall test:
```
expr true \& FIXEDSTRING : .*$1 \| false
```
Changing the `*` to `\*` (and costing a character) if there is a possibility that the command will be run in a directory where `.*$1` would match existing files.
The ordering is important here; `\&` evaluates to the *first* argument (not the second like in Python and similar language) when the overall "and" expression is true, so `true \&` must come before the pattern match test `FIXEDSTRING : .*$1`. The `.*` is needed because pattern matching is implicitly anchored at the beginning, as with `^`, so the `.*` lets it match anywhere. And no, sadly, you can't remove any of these spaces; `expr` expects each operand and operator to be a completely separate argument, so it doesn't need to do any tokenizing of its own, which means the spaces must be there so the shell can tokenize it.
For the specific case of a regex match, you can shorten it four more characters with `sed`, :
```
sed "/$1/ctrue
cfalse"<<<FIXEDSTRING
```
Similar uses allow replacing the various forms of numerical test and echo:
```
[ $1 -gt 0 ]&&echo yes||echo no
[[ $1>0 ]]&&echo yes||echo no
(($1>0))&&echo yes||echo no
```
with:
```
expr yes \& $1 \> 0 \| no
```
which, even with the need to escape the `>`, and despite numerical tests with `(())` allowing you to drop all whitespace, is still two characters shorter than the shortest of the naive approaches. If the comparison is more complex, you can mix-and-match:
```
expr yes \& $[$1>0] \| no
```
is the same length as the purely `expr`-based approach, but grows more slowly in more complex cases (as fewer things need escaping, and spaces aren't needed between terms).
[Answer]
When assigning noncontinuous array items, you can still skip the successive indices of continuous chunks:
```
bash-4.4$ a=([1]=1 [2]=2 [3]=3 [21]=1 [22]=2 [23]=3 [31]=1)
bash-4.4$ b=([1]=1 2 3 [21]=1 2 3 [31]=1)
```
The result is the same:
```
bash-4.4$ declare -p a b
declare -a a=([1]="1" [2]="2" [3]="3" [21]="1" [22]="2" [23]="3" [31]="1")
declare -a b=([1]="1" [2]="2" [3]="3" [21]="1" [22]="2" [23]="3" [31]="1")
```
According to `man bash`:
> 
> Arrays are assigned to using compound assignments of the form *name*=(value*1* ... value*n*), where each *value* is of the form [*subscript*]=*string*. Indexed array assignments do not require anything but *string*. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one.
> 
> 
> 
[Answer]
## Print the first word in a string
If the string is in the variable `a` and doesn't contain escape and format characters (`\` and `%`), use this:
```
printf $a
```
But it would be longer than the following code if it is needed to save the result into a variable instead of printing:
```
x=($a)
$x
```
]