LoginSignup
18
17

More than 3 years have passed since last update.

bashのコマンド実行結果を変数に格納する

Posted at

bash内でコマンドの実行結果を変数に格納して色々処理したかった

dateコマンドの実行結果を変数に格納する

com_date.sh
#!/bin/bash
com_date=$(date)
echo ${com_date}

ちなみにこんな感じで com_date = $(date) =の間にスペースを打つとエラーになる。

実行

$ sh com_date.sh
#=>2019年 10月 9日 水曜日 09:44:15 JST

psqlとかの実行結果も格納できる

#!/bin/bash
update_hoge=$(psql -h ${hoge_HOST} -p 5555 -U ${hege_USER} -d DBhoge -c "\
    UPDATE hogeSET hoge= '$string' WHERE hoge= 'hoge';\
    ")
echo ${update_hoge}

おわり

18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17