0

Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?

For eg., someone called it with script.sh --foo_option bar_arg

Is there a way to echo that exact command they typed from inside the script?

I've tried echo !! which does not work inside a script.

Curious Learner
  • 343
  • 2
  • 9
  • Very short form: No, you can't get back to the original text; all you can get is the array of C strings that it actually resulted in. However, for `script.sh --foo_option bar_arg`, the array of C strings (entries stored first in `"$0"` and then `"$@"`) is all you need. – Charles Duffy Apr 04 '19 at 17:33
  • @CharlesDuffy : `"$@"` does not give me `--foo_option` (only `bar_arg`) – Curious Learner Apr 04 '19 at 18:05
  • Show us a [mcve] that lets us reproduce the behavior ourselves. Either you're `shift`ing the argument off, or you're using an invocation method that consumes it (f/e, routing it to `$0` rather than `$1`, as would happen with `sh -c '...' --foo-option bar_arg`). – Charles Duffy Apr 04 '19 at 18:06

0 Answers0