Loom
Loom

Reputation: 9986

Starting tmux with certain command inside of bash script

I ran into problem when tried to start tmux inside of bash-script. The following script is sample of the problem.

tmux new-session -d -s main
tmux send-keys -t main 'ls ~/' C-m
tmux attach-session -d -t main

This script works correct - it started tmux with list of ~/ directory. Then I tried to start same command (ls ~/) as a variable

tmux new-session -d -s main
foo="'ls ~/'"
tmux send-keys -t main "$foo" C-m
tmux attach-session -d -t main

However, It didn't work. I have got the following message

'ls ~/'
$ 'ls ~/'
-bash: ls ~/: No such file or directory

What is the reason of this problem and how to fix it?

Upvotes: 0

Views: 261

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80921

You can't stick quotes inside quotes and have the shell remove them for you correctly.

See mywiki.wooledge.org/BashFAQ/050 for a full discussion on this.

Drop one set of the quotes there.

Either foo="ls ~/" or foo='ls ~/' but not both.

Upvotes: 1

Related Questions