Skip to content

Instantly share code, notes, and snippets.

@JeanCarlosChavarriaHughes
Created June 21, 2024 22:38
Show Gist options
  • Save JeanCarlosChavarriaHughes/e9bd316b697f4e61e84c9d01079c779c to your computer and use it in GitHub Desktop.
Save JeanCarlosChavarriaHughes/e9bd316b697f4e61e84c9d01079c779c to your computer and use it in GitHub Desktop.
#!/bin/bash
VARIABLE_NAME="NODE_VERSION"
VARIABLE_VALUE="21.7.1"
PROFILE_FILE="$HOME/.bash_profile"
# Check and append using awk
if ! awk -v var="$VARIABLE_NAME" 'BEGIN { found=0 } /^export/ { if ($0 ~ "export " var "=") found=1 } END { exit !found }' "$PROFILE_FILE"; then
echo "export $VARIABLE_NAME='$VARIABLE_VALUE'" >> "$PROFILE_FILE"
echo "Variable $VARIABLE_NAME added to $PROFILE_FILE"
else
echo "Variable $VARIABLE_NAME already exists in $PROFILE_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment