简体   繁体   中英

Passing bash output as argument to Python script

I am trying to pass Epoch time as an argument to my Python script but am struggling to figure out a way how. I would like to do something like the following:

epoch_time="date +%s" # computes Epoch time
./script.py epoch_time

I know that technically epoch_time would be considered a string there, but is there a way to invoke a command and store its output on one line like that?

Use $(cmd) to capture command output. Don't put any spaces around the = assignment.

epoch_time=$(date +%s)
./script.py "$epoch_time"

Or without the variable:

./script.py "$(date +%s)"

对于来这里寻找 bash 命令的任何人来说,这都有效

echo "foobar" | xargs -I{} ./script.py {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM