Pergunta

Quero anexar a uma string para que toda vez que eu o loop adicionará, digamos "teste" à string.

Como no PHP, você faria:

$teststr = "test1\n"
$teststr .= "test2\n"
echo = "$teststr"

Echos:

test1
test2

Mas eu preciso fazer isso em um script de shell

Foi útil?

Solução

No clássico SH, você tem que fazer algo como:

s=test1
s="${s}test2"

(Existem muitas variações nesse tema, como s="$s""test2")

Em Bash, você pode usar +=:

s=test1
s+=test2

Outras dicas

$ string="test"
$ string="${string}test2"
$ echo $string
testtest2
#!/bin/bash
message="some text"
message="$message add some more"

echo $message

Algum texto adiciona mais um pouco

teststr=$'test1\n'
teststr+=$'test2\n'
echo "$teststr"
VAR=$VAR"$VARTOADD(STRING)"   
echo $VAR
#!/bin/bash

msg1=${1} #First Parameter
msg2=${2} #Second Parameter

concatString=$msg1"$msg2" #Concatenated String
concatString2="$msg1$msg2"

echo $concatString 
echo $concatString2
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top