1

How would I get this to work in ksh?

variable1="$(strings /.sh_history | grep '^\user' | tail -1 | cut -d' ' -f2)"
variable2="$(command ${variable1})"

Basically, I would like to use variable1 as a string to use with a command to declare a variable.

UPDATE: The issue was not actually related to my syntax. The error was due to the text being formatted which created an issue. I cut using the text modifiers as delimiters and was able successfully pull the data. I did use both of the advised solutions to make my script cleaner.

2
  • Can you provide an example of the input and expected output. Commented Nov 21, 2017 at 12:29
  • variable1="$(strings /.sh_history | grep '^\user' | tail -1 | cut -d' ' -f2)" variable2="$(command ${variable1})" Basically, I would like to use variable1 as a string to use with a command to declare a variable.
    – poisonjam
    Commented Nov 21, 2017 at 13:25

2 Answers 2

3

If you want to store shell code, use functions:

function1() { strings /.sh_history | grep "$@"; }
variable1=$(function1 foo)
1

Use eval:

variable1='strings /.sh_history | grep'
variable2=$(eval "$variable1 foo")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .