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.

  • Can you provide an example of the input and expected output. – EightBitTony Nov 21 '17 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 Nov 21 '17 at 13:25
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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.