Echo (Bash)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

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