Binarydata
Binarydata

Reputation: 13

How do I combine a string and a variable into one command and execute it in bash?

To get into detail, I am writing a shell script to automate usage of a plugin. To do so, I am using xclip to pull a url from the x clipboard, then append it at the end of a command with arguments and execute the combined command.

I use url="$(xclip -o)" to get the url from the clipboard, then com='youtube-dl -x --audio-format mp3 ' to set the initial string. I've been clumsily stumbling through attempts at printf and defining new strings as str=$com $url (and many variants of such. I haven't written anything in a long time and know I'm screwing up something pretty basic. Anybody able to help?

Upvotes: 1

Views: 59

Answers (1)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19305

to concatenate two strings with a space in an assignment

str=$com' '$url

can be also written

str=$com" "$url

or

str="$com $url"

then the command can just be launched

$str

however

str=$com $url

is the syntax to call $url passing environment variable str=$com

also if url was a string which could contains spaces or tabs anan array should be used instead to avoid splitting when called

str=( $com "$url" )
"{str[@]}"

Upvotes: 2

Related Questions