
heredoc에 정의된 변수를 명령에 어떻게 전달할 수 있나요?
read -r -d '' tables <<'EOF'
table1
table2
table3
EOF
tables=$(tr '\n' ' ' < "$tables");
테이블 변수를 다음과 같이 정의하고 싶습니다.
table1 table2 table3
답변1
Bash를 사용하면 다음을 사용할 수 있습니다.여기 문자열
tables=$(tr '\n' ' ' <<< "$tables")
다른 쉘을 사용하면 다음을 사용할 수 있습니다.또 다른여기 문서
tables=$(tr '\n' ' ' << END
$tables
END
)
답변2
나는 보통 여러 줄의 문자열을 사용합니다.
tables="
table1
table2
table3"
echo $tables
for table in $tables; do echo $table; done
귀하는 heredoc
내 시스템에서 동등하게 취급됩니다.
답변3
여러 줄의 변수가 있으면 다음을 사용할 수 있습니다 echo
.
echo "$tables" | tr '\n' ' '
큰따옴표를 사용하세요.보호하다개행 문자. 비교하다:
$ echo $tables | tr '\n' '_'
table1 table2 table3_
와 함께:
$ echo "$tables" | tr '\n' '_'
table1_table2_table3_