1

I want to call the c program display_output with python generated arguments, however I'm not sure how to formulate the syntax. I tried this

./display_output (python -c "print 'A' * 20")

but I get

bash: syntax error near unexpected token `python' 

I suppose this goes along with my original question and could help me out with that. The only way I could find to try running python cmd line output as a bash command was by appending | bash to the command. However, is there a better way to do this?

(python -c "print 'ls'") | bash 

I clearly don't know my way around Bash, but I was certain there was a more appropraite way to do this.

5
  • Figured it out. I added quotes like so: ./display_output '(python -c "print 'A' * 20")' -_- Commented Apr 5, 2015 at 0:00
  • Nevermind, now have weird output. This doesn't work python -c "print 'A' * 20" | ./display_output Commented Apr 5, 2015 at 0:01
  • what is your c code? Commented Apr 5, 2015 at 0:01
  • It prints whatever is put into it. Commented Apr 5, 2015 at 0:03
  • still think you would find this easier to do using python itself. Commented Apr 5, 2015 at 0:06

1 Answer 1

2

When bash sees an open parenthesis at a place where a command can be, it will launch a subshell to run the enclosed commands. Where you currently have them is not a place where a command can go. What you want instead is command substitution

./display_output $(python -c "print 'A' * 20") # ...............^ 

You will get into trouble if any of the arguments generated contain whitespace (obviously not the case for this toy example.

To generate a string of 20 "A"s in bash, you would write:

a20=$(printf "%20s" "") # generate a string of 20 spaces # or, the less readable but more efficient: printf -v a20 "%20s" "" a20=${a20// /A} # replace all spaces with A's 

The last line is pattern replacement in shell parameter expansion

Sign up to request clarification or add additional context in comments.

4 Comments

I get an illegal instruction error with the first example.
What shell are you using? What do you get with echo $(python -c "print 'A' * 20")
@PadraicCunningham, follow the link and scroll down
Sorry, the illegal instruction error went away when I changed the amount of generated A's and flipped the double parentheses with singles, and vice versa.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.