1

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?

2 Answers 2

1

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)"
Sign up to request clarification or add additional context in comments.

5 Comments

That seems to work but it is passing !* into the Python script, which I assume is a special character. My Python script in this instance is expecting an integer value.
Ah, just had to remove the quotes. ./script.py $(date +%s) works for me. Thank you kindly!
Nope, don't remove the quotes.
How come? I need an integer, not a string representation of the integer.
Everything's a string in shell scripts. There are no integer values, only string representations of them. See tldp.org/LDP/abs/html/quotingvar.html for details on why expansions should be quoted.
0

For anyone who came here looking for a bash command, this works

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.