23

I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters.

Meaning somethings like this:

special_execute()
{
    # Some code

    # Here's the point where the command gets executed
    $@

    # More code
}

special_execute echo "abc"

I already tried I $@, "$@", $*, "$*" how could I do that?

8
  • $@ works for me.. special() { $@; } ... special echo "foo" gives foo
    – Drav Sloan
    Aug 28, 2013 at 0:54
  • It did not work for me with: perl -MTime::HiRes=sleep -le 'for(1..100) { print; sleep 0.05; }' and tar -cvf "backups/test.tar" -P "backups/uncompressed_server_backup_(DO NOT TOUCH!)/server/"
    – BrainStone
    Aug 28, 2013 at 0:55
  • 3
    use quotes around "$@", then you will have more success :)
    – Drav Sloan
    Aug 28, 2013 at 0:59
  • Ok. Now it works. it seems like I messed up the code arround the actual call.
    – BrainStone
    Aug 28, 2013 at 1:04
  • 1
    no problem, we all have those moments :)
    – Drav Sloan
    Aug 28, 2013 at 1:06

1 Answer 1

15

I think it's just a quoting issue when you're passing the arguments into the function.

Try calling it like so:

$ special_execute "echo 'abc'"
'abc'

If you don't want the single quotes around abc then change the quoting like this:

$ special_execute "echo abc"
abc

Debugging

You can wrap the internals of the function so that it echoes out with more verbosity.

$ function special_execute() { set -x; "$@"; set +x; }

Then when you run commands through the function, special_execute you can see what's going on.

ps example:

$ special_execute ps -eaf
+ ps -eaf
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Aug21 ?        00:00:01 /sbin/init
root         2     0  0 Aug21 ?        00:00:00 [kthreadd]
...

perl example:

$ special_execute perl -MTime::HiRes=sleep -le 'for(1..10) { print; sleep 0.05; }'
+ perl -MTime::HiRes=sleep -le 'for(1..10) { print; sleep 0.05; }'
1
2
3
4
5
6
7
8
9
10
+ set +x

Parsing argument $1

You could do something like this to parse any arguments passed in as $1.

$ function special_execute() { 
    [ "$1" -eq "-123" ] && echo "flagY" || echo "flagN"; 
    shift; 
    set -x; "$@"; set +x; 
  }

Example

with debugging enabled:

$ special_execute -123 perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
flagY
+ perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
1
2
3
4
5
+ set +x

with debugging off - -123:

$ special_execute -123 perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
flagY
1
2
3
4
5

with debugging off - -456:

$ special_execute -456 perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
flagN
1
2
3
4
5
2
  • Why do I get an error saying 'command not found' when using aliases?
    – Elie G.
    Aug 14, 2019 at 15:49
  • @DrunkenPoney - no such cmd, it's alias.
    – slm
    Aug 14, 2019 at 18:49

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.