Echo (Bash): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(4 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 29: 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 ==
 
== Bronnen ==

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 '.'

Zie ook

Bronnen