Running a variable as a command.

Need help with C, C++, perl, python, etc?

Running a variable as a command.

Postby Munty Scruntfundle » 2018-11-27 20:10

Hi folks.

A quick question if I may, I'm slowly getting used to bash, but my experience with mid level languages is very limited.

If I try this;
-# ssh -t user@ 'sudo su; bash -l'
I get prompt in the terminal logged on as su. I'm not actually going to use this, well, maybe I will I'm not sure.

But if in a script I say;
sudo="'sudo su; bash -l'"
sh="ssh -t"
run="$ssh $address $sudo"
echo $run

the output is;
-# ssh -t user@ 'sudo su; bash -l'
bash: sudo su; bash -l: command not found - And variations of command not found with exec and and everything else I've tried. However, if I copy and paste the echo output and run it as a command it runs perfectly.

So where is the command going wrong when running it, and what have I done wrong? I have an idea the output is being run as one long command line, so it needs formatting, but I really don't know.

Many thanks.
Munty Scruntfundle
Posts: 54
Joined: 2018-11-27 16:53

Re: Running a variable as a command.

Postby Head_on_a_Stick » 2018-11-28 06:00

Don't break DebianHow to report bugs

SharpBang GNU/Linux — a pre-configured Openbox/Tint2 desktop running on Debian stable
User avatar
Posts: 10680
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Running a variable as a command.

Postby debiman » 2018-11-28 06:37

Munty Scruntfundle wrote:`run`

have you just tried
Code: Select all


i have done this sometimes successfully, sometimes, in cases like your's where variables are nested like that, it took some fiddling to make it work.

depending on who you ask, this is an evil thing to do and eval should be used instead, or the other way around, so take it with a grain of salt and be aware of possible security implications.

btw, what is the purpose of this?
quickly starting a root shell over ssh? is separately typing "su" too much for you?
you know it will still ask your password, right?
even so, it seems vaguely unsafe and unnecessary.
User avatar
Posts: 3064
Joined: 2013-03-12 07:18

Re: Running a variable as a command.

Postby xepan » 2018-11-28 06:46

The answer is easy: Don't.
Variables hold data. Functions hold code. Don't put code inside variables!

from here

Once at it make sure to read the common pitfalls:

And on a last note:
08:32 <greybot> Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK. Instead,
ask a good question here or refer to the Guide and FAQ (see topic)

08:32 <greybot> Like virtually every other web site you find via Google, StackOverflow is chock full of wrong answers. Verify everything you read
there before using it.

Same is valid for forums, as the people who answer will do a quick search and post what they have found.
Use the above guide or bash-hackers instead:
Posts: 89
Joined: 2018-11-28 06:38

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest