Differences between revisions 1 and 7 (spanning 6 versions)
Revision 1 as of 2007-05-02 22:56:56
Size: 226
Editor: redondos
Comment:
Revision 7 as of 2011-04-12 20:01:06
Size: 1885
Editor: Lhunath
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq12)]]
== A program (e.g. a file manager) lets me define an external command that an argument will be appended to - but i need that argument somewhere in the middle... ==
<<Anchor(faq12)>>
== How do I invoke a shell command from a non-shell application? ==

You can use the shell's `-c` option to run the shell with the sole purpose of executing a short bit of script:
Line 4: Line 6:
    sh -c 'echo "$1"' -- hello     sh -c 'echo "Hi! This is a short script."'
Line 6: Line 8:

This is usually pretty useless without a means of passing data to it. The best way to pass bits of data to your shell is to pass them as positional arguments:
{{{
    sh -c 'echo "Hi! This short script was run with the arguments: $*"' -- "foo" "bar"
}}}

Notice the -- before the actual positional parameters. The first argument you pass to the shell process (that isn't the argument to the `-c` option) will be placed in `$0`. Positional parameters start at `$1`, so we put a little placeholder in `$0`. This can be anything you like; in the example, we use the generic `--`.

This technique is used often in shell scripting, when trying to have a non-shell CLI utility execute some bash code, such as with [[UsingFind|find(1)]]:
{{{
    find /foo -name '*.bar' -exec bash -c 'mv "$1" "${1%.bar}.jpg"' -- {} \;
}}}

Here, we ask `find` to run the `bash` command for every `*.bar` file it finds, passing it to the `bash` process as the first positional parameter. The `bash` process runs the `mv` command after doing some [[BashFAQ/073|Parameter Expansion]] on the first positional parameter in order to rename our file's extension from `bar` to `jpg`.

Alternatively, if your non-shell application allows you to set environment variables, you can do that, and then read them using normal variables of the same name.

Similarly, suppose a program (e.g. a file manager) lets you define an external command that an argument will be appended to, but you need that argument somewhere in the middle. In that case:
{{{
    #!/bin/sh
    sh -c 'command foo "$1" bar' -- "$@"
}}}

----
CategoryShell

How do I invoke a shell command from a non-shell application?

You can use the shell's -c option to run the shell with the sole purpose of executing a short bit of script:

    sh -c 'echo "Hi!  This is a short script."'

This is usually pretty useless without a means of passing data to it. The best way to pass bits of data to your shell is to pass them as positional arguments:

    sh -c 'echo "Hi! This short script was run with the arguments: $*"' -- "foo" "bar"

Notice the -- before the actual positional parameters. The first argument you pass to the shell process (that isn't the argument to the -c option) will be placed in $0. Positional parameters start at $1, so we put a little placeholder in $0. This can be anything you like; in the example, we use the generic --.

This technique is used often in shell scripting, when trying to have a non-shell CLI utility execute some bash code, such as with find(1):

    find /foo -name '*.bar' -exec bash -c 'mv "$1" "${1%.bar}.jpg"' -- {} \;

Here, we ask find to run the bash command for every *.bar file it finds, passing it to the bash process as the first positional parameter. The bash process runs the mv command after doing some Parameter Expansion on the first positional parameter in order to rename our file's extension from bar to jpg.

Alternatively, if your non-shell application allows you to set environment variables, you can do that, and then read them using normal variables of the same name.

Similarly, suppose a program (e.g. a file manager) lets you define an external command that an argument will be appended to, but you need that argument somewhere in the middle. In that case:

    #!/bin/sh
    sh -c 'command foo "$1" bar' -- "$@"


CategoryShell

BashFAQ/012 (last edited 2019-06-06 17:29:00 by GreyCat)