Programmatically add functions to a user's bash profile [duplicate] - bash

This question already has answers here:
How to install a bash function containing variables using a bash script? [duplicate]
(2 answers)
Create a script that adds lines of code to .bashrc then reloads the terminal
(2 answers)
Closed 4 years ago.
I've written a very simple utility to allow a couple of colleagues to easily access some system logs.
The dependencies for this are installed by running a curl to an install.sh file on GitHub.
There are a couple of functions and aliases that are handy to have in your bash profile. Out of interest how would I programmatically add items to a user's bash profile in a shell script.

Related

Why a function I am sourcing in bash not access the environment variables I have set? [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
$BASH_VERSION reports old version of bash on macOS, is this a problem that should be fixed?
(4 answers)
Closed 10 months ago.
I have some code that looks like this:
export AWS_REGION='us-east-1'
export CLUSTER_NAME='my_cluster'
export PROJECT_NAME='my_project'
source ./utils.sh
...additional functions...
annotate_cluster # comes from utils.sh
annotate_cluster, which comes from utils.sh, relies on the environment variable PROJECT_NAME. However, when I run it, it complains _utils.sh: line 60: FOO-${PROJECT_NAME^^}: bad substitution. Why can it not access the environment variable I have set?

Ways to execute bash script without "./"? [duplicate]

This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
How to enable a system-wide function for users including sudo?
(2 answers)
Closed 3 years ago.
I have a bash script called climb.sh. When I execute it I write
./climb.sh 1
while inside the directory in which the script is located. However, I want to do the same thing wherever I am, and across all shell sessions by simply calling
climb 1
Also, climb.sh takes an numeric argument and calls "cd ../" that many times. In order for the program to work, it has to run alongside the current process, not within some child process.
How to achieve all this?
Thanks

why bash shell does not make any difference after executed? [duplicate]

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 5 years ago.
I'm working a small project which needs using OpenMPI to make "mpicc" work.
I made a file make_cmd:
#!/bin/bash
module load OpenMPI
However, after executing ./make_cmd, I was told:
mpicc: command not found
But if I just type on the command line: module load OpenMPI, then mpicc is working.
Why is that? Thanks!
See this answer on neighbouring site.
Because module is an alias/shell function and not a binary program, it's not necessarily available in the non-interactive sub-shell that is created when you run your script. You could probably run source make_cmd though, as that will just run the commands in your current interactive shell. You could ditch the #!/bin/bash line in that case.

Python3: equivalent of "export VARNAME" in terminal [duplicate]

This question already has answers here:
How to set environment variables in Python?
(19 answers)
Reading and writing environment variables in Python? [duplicate]
(4 answers)
set environment variable in python script
(3 answers)
Closed 5 years ago.
In terminal (bash) on OSX I can set an environment variable, using the syntax export VARNAME=1 or export VARNAME="hello"; and that persist as long as the session is running, or until the terminal window is closed.
What would be the equivalent form, to do the same via Python3? I would like to avoid to call Popen just to set a global variable.
Also I need this variable only for the purpose to run my python code; once the script is done, I do not need it anymore; so even if it last only for the lifespan of my script running, it is acceptable.

Is it mandatory to use #!/bin/bash even inside bash subshell [duplicate]

This question already has answers here:
Writing a Bash script without the shebang line
(2 answers)
Bash script execution with and without shebang in Linux and BSD
(2 answers)
Closed 5 years ago.
If after logging into my system I type: bash (to use bash subshell) and then try to run a bash script (e.g. example.sh), then does it matter if I do not put #!/bin/bash as the first line of the script or it is fine since I am already inside bash subshell?

Resources