Echo (Bash): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(2 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 30: Regel 30:
 
</pre>
 
</pre>
  
== Strip output from commands ==
+
== Strip whitespace around output ==
  
There are some nifty tricks to make the output of commands less messy. Some examples:
+
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>
 
<pre>
Regel 51: Regel 51:
 
your_command | grep -o '.'
 
your_command | grep -o '.'
 
</pre>
 
</pre>
 
or
 
  
 
== Zie ook ==
 
== Zie ook ==
  
 +
* [[Grep]]
 +
* [[Pipelining & redirection (Bash)]]
 
* [[printf]]
 
* [[printf]]
  

Huidige versie van 24 aug 2023 om 18: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