1

I'm using Visual Studio Code 1.39.2 on Ubuntu 18.04 to debug a bash script.

In my environment .bashrc file, I have defined a function hello1:

function hello1
{
    echo Hello from .bashrc!
}

In my bash script, I've added another function hello2:

function hello2
{
    echo Hello from your script!
}

and the script contains the lines:

hello1
hello2

Now set the following launch configuration:

{
    "type": "bashdb",
    "request": "launch",
    "name": "Debug: Active script",
    "program": "${file}"
}

Before debugging, enter the Terminal window within VS code. Type hello1 and the output is Hello from .bashrc!. Then type hello2 and as expected, Command 'hello2' not found is the result because this function is not known to the bash shell. All good.

Now debug from within VS Code (choose the Debug: Active script configuration with the script active). Watch the Debug Console window as you step through the script. This time hello2 works but you get an error for hello1:

line X: hello1: command not found

How do you make bashdb and the Debug Console window understand environment and functions from the user .bashrc?

3
  • 1
    The two declare -F lines aren't doing anything useful. Functions are not inherited (by default) from the environment of a parent shell. Don't think of .bashrc as a general repository for common code; it's meant to initialize your interactive shell, nothing more. If your script needs hello1, define it in the script, or have the script source a file that defines it. Commented Nov 1, 2019 at 17:02
  • @chepner: That's helpful, thanks. Have removed the declare -F calls. They were there just as another way to call the two functions. How to get your user functions and aliases included in your script? Do you mean something like putting all common code in a file called (say) .bashrc_usr, then source ~/.bashrc_usr in your script? Tried that and it appears to work for functions, but not aliases. Commented Nov 1, 2019 at 17:15
  • The aliases are defined, but alias expansion is only enabled by default for interactive shells. Commented Nov 1, 2019 at 18:46

1 Answer 1

1

It works when you export the function:

function hello1
{
  echo Hello from bashrc
}
export -f hello1

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.