Services Blog Français

Bash interactive scripting basics

| by jpic | linux bash

A variable looks like this:

    export FOO=bar

To get a variable in your interactive shell, source the script that contains it as such:

    source script_that_contains_FOO
    echo $FOO

A function looks like this:

    function foo() {
        echo foo

To run a function in your interactive shell, source the script and call the function like this:

    source script_that_contains_foo

To debug something that’s wrapped in a bash function or script, set the -x option. To de-activate debugging, set +x. Example:

    [env] 04/02 2014 02:17:26 jpic@etta /home/jpic 
    $ source script_that_contains_foo 

    [env] 04/02 2014 02:17:29 jpic@etta /home/jpic 
    $ set -x; foo; set +x
    + foo
    + echo bar
    + set +x

Lines prefixed with a + sign are those that are executed by bash. Lines without + prefix correspond to output.

Ok, now you can use shell scripting like an interactive framework.

They trust us