I write a script on bash.
It opens a file in which bash commands that need to be executed are stored line by line.
Suppose I have a variable in which I saved the line

ls -l 

How to make it run?

Wait i do first

 a=\`cat ..\folder\file\` 

Well, then b = $a refuses to work (there is one line in the file).

I want to scatter commands from a file into an array, and then, at my discretion, execute them. Each variable will have one command, for example, "grep blablabla", "touch xxx", etc.

  • You better write what you want to receive , i.e. Why do you do all this? And then there is a lot of incomprehensible, then you have backward quotes in the text (which is in a variable), then no. Options can be quite a lot. So, let's be more specific. And the simplest thing is to not execute this file (as a script)? - avp
  • @avp, as I understand it, backquotes here are a way to insert code into the text on this site. In the e-mail can be seen on the page - no. - alexlz
  • So I can not understand what the author really wants. - avp

2 answers 2

eval

  a='ls -l' eval $a 
  • as an option: a = ls -l only difference is that in this case the command will be executed before assignment - jmu
  • But first in the question the contents of the variable it was the text in reverse quotes ie avp @ avp-xub11: ~ / src / yamsort $ echo $ a ls -l avp @ avp-xub11: ~ / src / yamsort $ So, here. Something simple (without “editing” the contents of a tr-th or sed-th) does not work. And c = 'ls -l' i.e. ... $ echo $ c ... $ ls -l and just $ c (without eval) works. Actually, eval is needed only for substitution inside. - avp
  • @jmu, the fundamental difference is the contents of the variable a. In case a = ls - echo $ a will print the composition of the current table of contents, and for a = 'ls -l' the line ls -l - avp natural
  • > So, here. Something simple (without “editing” the contents of a tr-th or sed-th) does not work. You can do the same: a = 'ls -l' echo $a UPD. and, or you about return quotes in the variable meant? - Ilya Pirogov
  • @Ilya Pirogov, I'm talking about the contents of a, which is equal to `ls -l` (that is, in a text with backquotes ). - avp

I want to scatter commands from the file into the array, and then, at my discretion, execute them

In this case, I think it is easier to scatter commands in variables:

 # файл commands CMD_GREP="grep блаблабла $@" CMD_TOUCH="touch xxx" CMD_LS="ls -l" 

And then just "import" them and use:

 source commands $CMD_GREP test_file $CMD_LS 
  • one
    Isn't it easier to write all this with aliases and not suffer with dollars? - alexlz
  • I agree, you can alias. - Ilya Pirogov
  • source is from csh or tcsh. In sh (bash) this is koiaanda . (point). Yes, the easiest way is to write commands (and maybe some variables) in a file and execute it (via `. ') In the current script, and then use these (new) variables (and / or aliases). - avp
  • @avp, in source also can be used. The same point - skegg
  • Exactly (checked), but I did not know. Thank. - avp