Home > database >  Having trouble with declared functions in `~/.profile` not being loaded in integrated terminal shell
Having trouble with declared functions in `~/.profile` not being loaded in integrated terminal shell

Time:04-28

I have a .profile which is copied to /home/vscode during my container build before running common-debian.sh

This is important as during container image build common-delian.sh will skip creating a default .profile if the file is already present

In that profile I have a number of bash functions and some exported variables.

One of the variables relies on executing one of the declared functions, and in my shell I can see the correct variable and value is set.

This problem arises when opening a new integrated terminal in vscode. "new terminal" command

However none of my functions are available in the shell to execute. Confirmed through typeset -F

I can run source ~/.profile and the functions are then available in the shell.

Is there some place I can turn up logging to determine what is happening here?

Details about the install

vscode Host install is Mac M1

VERSION='0.202.6'
CONTENTS_URL='https://github.com/microsoft/vscode-dev-containers/tree/main/containers/debian/history/0.202.6.md'
DEFINITION_ID='debian'
VARIANT='bullseye'
GIT_REPOSITORY='https://github.com/microsoft/vscode-dev-containers/'
GIT_REPOSITORY_RELEASE='v0.224.0'
BUILD_TIMESTAMP='Fri, 25 Feb 2022 10:48:36 GMT'

Example function declaration

kconfig() {
 ls $HOME/.kube/configs/* | tr '\n' ':'
}
declare -f kconfig

Example variable making use

export KUBECONFIG="$(kconfig)"

I can confirm before sourcing .profile, this variable is set and valid and should only have come from the .profile declaration.

home folder for reference

vscode ➜ /workspace (master ✗) $ ls -al ~
total 76
drwxr-xr-x  1 vscode vscode  4096 Apr 27 18:59 .
drwxr-xr-x  1 root   root    4096 Feb 25 10:55 ..
drwxr-xr-x  1    501 dialout  160 Mar  8 22:31 .aws
-rw-r--r--  1 vscode vscode   220 Aug  4  2021 .bash_logout
-rw-r--r--  1 vscode vscode  4665 Feb 25 10:55 .bashrc
drwxr-xr-x  3 vscode vscode  4096 Apr 27 18:59 .cache
drwxr-xr-x  1    501 dialout  192 Apr 25 21:41 .config
drwxr-xr-x  1    501 dialout  160 Apr 27 14:20 .creds
-rw-r--r--  1 vscode vscode   500 Apr 27 18:59 .gitconfig
drwx------  2 vscode vscode  4096 Apr 27 18:59 .gnupg
drwxr-xr-x  1    501 dialout  160 Mar  9 21:59 .kube
drwxr-xr-x 12 vscode vscode  4096 Feb 25 10:55 .oh-my-zsh
-rw-r--r--  1 vscode vscode  2381 Apr 27 19:19 .profile
drwxr-xr-x  1 vscode root    4096 Apr 27 18:59 .vscode-server
drwxr-xr-x  3 vscode root    4096 Apr 27 18:26 .vscode-server-insiders
-rw-r--r--  1 vscode vscode  3897 Feb 25 10:55 .zshrc

Edit below:


Now I am copying this file in as ~/.bash_profile There is still a .profile, vscode common-debian.sh creates this if the file is not present when building the container image.

vscode ➜ /workspace (master ✗) $ ls -al ~
total 80
drwxr-xr-x  1 vscode vscode  4096 Apr 27 20:08 .
drwxr-xr-x  1 root   root    4096 Feb 25 10:55 ..
drwxr-xr-x  1    501 dialout  160 Mar  8 22:31 .aws
-rw-r--r--  1 vscode vscode   220 Aug  4  2021 .bash_logout
-rw-r--r--  1 vscode vscode  2671 Apr 27 19:57 .bash_profile
-rw-r--r--  1 vscode vscode  4665 Feb 25 10:55 .bashrc
drwxr-xr-x  3 vscode vscode  4096 Apr 27 20:08 .cache
drwxr-xr-x  1    501 dialout  192 Apr 25 21:41 .config
drwxr-xr-x  1    501 dialout  160 Apr 27 14:20 .creds
-rw-r--r--  1 vscode vscode   500 Apr 27 20:08 .gitconfig
drwx------  2 vscode vscode  4096 Apr 27 20:08 .gnupg
drwxr-xr-x  1    501 dialout  160 Mar  9 21:59 .kube
drwxr-xr-x 12 vscode vscode  4096 Feb 25 10:55 .oh-my-zsh
-rw-r--r--  1 vscode vscode   807 Aug  4  2021 .profile
drwxr-xr-x  1 vscode root    4096 Apr 27 20:08 .vscode-server
drwxr-xr-x  3 vscode root    4096 Apr 27 20:07 .vscode-server-insiders
-rw-r--r--  1 vscode vscode  3897 Feb 25 10:55 .zshrc

also added the following to /etc/profile.d/

# This file is intended to be copied into /etc/profile.d
project_profile="$HOME/.bash_profile"
if [ -r $project_profile ]; then
  source $project_profile
else
  echo "File not found or not readable: $project_profile"
fi

none of which has had an effect on the resulting new shells perhaps ignoring vscode docker exec -it {container} /bin/bash still has the same results.

I have also tried creating an empty terminal profile with an empty shell profile, launching vscode from that terminal window. Thinking that perhaps inherited env was causing issues.

Also set the following user setting "terminal.integrated.inheritEnv": false,

CodePudding user response:

From the man page for bash (in the INVOCATION section):

When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following com‐ mand were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

but the value of the PATH variable is not used to search for the file‐ name.

In your use case, i.e. a shell script, no startup file is read. But you can change that by setting the value of BASH_ENV to a file you want to read on startup. Alternately your script can source anything you like when it runs, e.g. source .profile.

  • Related