Echo (Bash): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(5 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 1: | Regel 1: | ||
+ | == Newline == | ||
+ | |||
+ | Er is geen eenduidige manier om met <code>echo</code> een ''newline'' te generen. Oa. daarom is <code>printf</code> waarschijnlijk beter dan <code>echo</code> [https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo/65819#65819] | ||
+ | |||
== Variabelen == | == Variabelen == | ||
Regel 25: | Regel 29: | ||
tekst1hoitekst2 | tekst1hoitekst2 | ||
</pre> | </pre> | ||
+ | |||
+ | == Strip whitespace around output == | ||
+ | |||
+ | There are some nifty tricks to make the output of commands less messy. They seem to involve <code>echo</code> and when I was looking for this around 2023.08, I looked here first. Some examples: | ||
+ | |||
+ | <pre> | ||
+ | echo "Name (2): $(wp --user=4 wc product_attribute_term get $taxid $i --field=name --format=csv | grep .)" | ||
+ | </pre> | ||
+ | |||
+ | The part <code>|grep .</code> actually strips the messy things. | ||
+ | |||
+ | More: [[Pipelining & redirection (Bash)#Trim whitespace around output]] | ||
+ | |||
+ | Some alternatives (didn't try them out): | ||
+ | |||
+ | <pre> | ||
+ | echo -n "$(your_command)" | ||
+ | |||
+ | echo -n "$(your_command)" | tr -d '[:space:]' | ||
+ | |||
+ | your_command | grep -o '.' | ||
+ | </pre> | ||
+ | |||
+ | == Zie ook == | ||
+ | |||
+ | * [[Grep]] | ||
+ | * [[Pipelining & redirection (Bash)]] | ||
+ | * [[printf]] | ||
+ | |||
+ | == Bronnen == | ||
+ | |||
+ | * https://unix.stackexchange.com/questions/219268/how-to-add-new-lines-when-using-echo/219274#219274 | ||
+ | * https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo/65819#65819 |
Huidige versie van 24 aug 2023 om 20:35
Newline
Er is geen eenduidige manier om met echo
een newline te generen. Oa. daarom is printf
waarschijnlijk beter dan echo
[1]
Variabelen
Bv.:
#!/bin/bash # a="hoi" echo $a echo "$a" echo $a"hallo-1" echo "$a""hallo-2" echo "$ahallo-3 " echo "tekst1""$a""tekst2"
Output:
hoi hoi hoihallo-1 hoihallo-2 -3 tekst1hoitekst2
Strip whitespace around output
There are some nifty tricks to make the output of commands less messy. They seem to involve echo
and when I was looking for this around 2023.08, I looked here first. Some examples:
echo "Name (2): $(wp --user=4 wc product_attribute_term get $taxid $i --field=name --format=csv | grep .)"
The part |grep .
actually strips the messy things.
More: Pipelining & redirection (Bash)#Trim whitespace around output
Some alternatives (didn't try them out):
echo -n "$(your_command)" echo -n "$(your_command)" | tr -d '[:space:]' your_command | grep -o '.'