Skip to content

Instantly share code, notes, and snippets.

@its-dibo
Last active August 1, 2022 01:36
Show Gist options
  • Save its-dibo/21cc0a0b64967232e342f5b8c1f566fe to your computer and use it in GitHub Desktop.
Save its-dibo/21cc0a0b64967232e342f5b8c1f566fe to your computer and use it in GitHub Desktop.
customizing Google cloud shell

to customize Google cloud shell, modify on of the following files:

  • $HOME/.customize_environment
  • $HOME/.profile
  • $HOME/.bashrc

notes

  • .customize_environment runs once when google shell instance boots up,
  • .profile, .bashrc run once for each shell login, i.e: when a new shell opened they will run, use to customise the shell prompt or to add environments.
  • if .bash_profile exists .profile, .bashrc will not executed
  • when .customize_environment runs, not all parts (like npm) are available yet or not installed yet.
  • it outputs it's log to /var/log/customize_environment, and touches /google/devshell/customize_environment_done
  • welcome message is located in /etc/profile.d/init_help.sh
  • to customise the prompt modify PS1 (in $HOME/.bashrc or $HOME/.profile for example), override the value in /google/devshell/bashrc.google
  • any file outside of $HOME will be reset every on restart https://stackoverflow.com/a/67155805/12577650
  • start .customize_environment with #!/bin/sh

more details:

@its-dibo
Copy link
Author

content of: /etc/profile.d/init_help.sh

note:
this file is outside of $HOME dir, so it will be reset every restart
todo: use another file inside of $HOME

DATE=$(date +%Y%m%d)

echo $DATE

[ -z "${SUDO_USER}" ] &&
    echo "Welcome to Cloud Shell! "

# $DEVSHELL_PROJECT_ID: Google cloud project id
if [[ -n $DEVSHELL_PROJECT_ID ]]; then
    echo -e "project: ${DEVSHELL_PROJECT_ID}"
else
    echo -e 'To set your Cloud Platform project, run `gcloud config set project [PROJECT_ID]`'
fi

if [[ -n "$ACTIVE_CUSTOM_IMAGE" ]]; then
    echo -e "Docker image ${$ACTIVE_CUSTOM_IMAGE}\n\n"
fi


if [[ -n $API_DOGFOOD ]]; then
    echo -e 'DOGFOOD mode...'
fi

@its-dibo
Copy link
Author

its-dibo commented Apr 19, 2021

# customise the prompt
export PS1="\e[0;37m\#\e[m \u\e[1;37m(${DEVSHELL_PROJECT_ID})\e[m \e[1;33m\w:\e[m "

equivilant to:
lineNumber:white -> userName: white,bold -> (projectId):green -> currentPath:yellow,bold

explanation:

color code: \e[0;37m{{VALUE}}\e[m
\e[ : start color code
0;37m : type & color (ex: 1=bold; 37=white)
{{value}}
\e[m :close color code

values:

#: line number
\u: username
${DEVSHELL_PROJECT_ID}: google cloud project id
\w: current path

about PS1: https://linoxide.com/change-bash-prompt-variable-ps1/
colors: https://linoxide.com/change-linux-shell-prompt-with-different-colors/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment