Export variable in remote .bashrc [duplicate] - bash

This question already has an answer here:
How have both local and remote variable inside an SSH command
(1 answer)
Closed 1 year ago.
Using Linux and bash:
in remote .bashrc file I've export FOO="hello"
locally I run this command but none variable was showed
ssh user1#192.168.1.114 ". /home/user1/.bashrc && echo \$FOO"
How could I get remote FOO variable?

I suggest with bash:
source <(ssh user1#192.168.1.114 cat /home/user1/.bashrc)
declare -p FOO
Output:
declare -x FOO="hello"
I assume /home/user1/.bashrc contains export FOO="hello".

Related

How to make a permanent environment variable in a make bash script? [duplicate]

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Makefile variable assignment error in echo
(2 answers)
Closed 1 year ago.
Attempting to set a user environment variable using input from a user in a bash script running in a makefile.
all:
if[ ! $(ENV_VAR) ] ; then \
read -p "What the variable?" env_var; \
if [[ $$env_var == valid_value || $$env_var == other_valid_value ]]; then \
echo export ENV_VAR=$$env_var >> /home/userName/.bashrc; \
source /home/userName/.bashrc \
else \
echo $$env_var is an invalid value\; Try again; \
fi \
fi
This code doesn't work. Is there any way to run source in a bash script?
Here's an example of the output:
What the variable? valid_value
What the variable? valid_value
What the variable? valid_value
...
After running the script the ~/.bashrc file did get the appropriate export command. Running source after the script does make and environment variable I need. I just need it to run in the make script.

Change directory in script bash [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Find file then cd to that directory in Linux
(12 answers)
Make a Bash alias that takes a parameter?
(24 answers)
Closed 2 years ago.
I want to find a file and goto his directory. I made a bash script :
#!/bin/bash
FILE=$1
FILEPATH=`find . -name "$FILE"`
if [ -f "$FILEPATH" ]
then
cd $(dirname "$FILEPATH")
fi
But this script does not work. I saw on this post that I have to add exec bash or $SHELL but it create a new bash prompt ans display my welcome message.
How can I do ? I just want a script, alias or something to find a file and go to the directory containing that file.
Source your script instead of running it like you do. When you run it like you do, you spawn a new shell that executes the cd, completes succesfully, closes the shell and returns to your current shell, leaving you in your pwd.
Use source myscript.sh or . myscript.sh instead of bash myscript.sh or myscript.sh.

Why does "set VAR=value" not work in bash? [duplicate]

This question already has an answer here:
Why does sourcing a script with "set var = value" break $#?
(1 answer)
Closed 3 years ago.
I am having problem setting a environment variable correctly.
These are first few lines of my deploy.sh:
if [[ -z ${PEM_PATH+x} ]]; then
printf "Please set the PEM_PATH environment variable\n"
exit 1
fi
This is my terminal output:
bash-3.2$ set PEM_PATH="/Users/Elasticsearch/Desktop/ec2-poc.pem"
bash-3.2$ ls
Jenkinsfile bps-dashboard.iml mvnw node_modules package.json src webpack.config.js
README.md deploy.sh mvnw.cmd package-lock.json pom.xml target
bash-3.2$ set PEM_PATH="/Users/Elasticsearch/Desktop/ec2-poc.pem"
bash-3.2$ echo $PEM_PATH
/Users/Elasticsearch/Desktop/ec2-poc.pem
bash-3.2$ sh deploy.sh
Please set the PEM_PATH environment variable
bash-3.2$
What am I doing wrong here ?
set does not set variables in bash (or other POSIX-family shells) -- it configures shell option flags, or changes the active argument list ($1, $2, and so on).
You can run any of the following to define PEM_PATH as an environment variable (without the export or the -x argument to declare or the use of set -a it would be a regular, non-exported shell variable):
export PEM_PATH="/Users/Elasticsearch/Desktop/ec2-poc.pem" -- both defining and exporting the variable with a single command.
PEM_PATH="/Users/Elasticsearch/Desktop/ec2-poc.pem"; export PEM_PATH -- first defining PEM_PATH as a regular shell variable, then promoting it to an environment variable
declare -x PEM_PATH="/Users/Elasticsearch/Desktop/ec2-poc.pem" -- using the bash-only extension declare to define PEM_PATH as an exported variable.
set -a; PEM_PATH="/Users/Elasticsearch/Desktop/ec2-poc.pem"; set +a -- using set -a to make all variables exported to the environment by default; then setting the variable; then turning off that flag.

getting variable name in file name for bash [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 4 years ago.
I wanted to change the name of my file from file.txt to file_4i.txt and file_5i.txt according to the number I need but when I use the command below, the file name changes to file_.txt and the value of m never is indicated. I wanted to get 4i but $mi does not work either.
sudo sh -c "m=4 ; mv file.txt file_$mi.txt"
sudo sh -c "m=4 ; mv file.txt file_$m.txt"
Use single quotes so the variable doesn't expand early, and use {} so mi isn't interpreted as the variable name:
sudo sh -c 'm=4 ; mv file.txt file_${m}i.txt'
sudo sh -c 'm=4 ; mv file.txt file_$m.txt'

ssh command execution doesn't consider .bashrc | .bash_login | .ssh/rc? [duplicate]

This question already has answers here:
Why aliases in a non-interactive Bash shell do not work
(4 answers)
Closed 6 years ago.
I am trying to execute a command remotely over ssh, example:
ssh <user>#<host> <command>
The command which needs to be executed is an alias, which is defined in .bashrc, e.g.
alias ll='ls -al'
So what in the end the following command should get executed:
ssh user#host "ll"
I already found out that .bashrc only gets sourced with interactive shell, so in .bash_login I put:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
and I also tried to define the alias directly in .bash_login.
I also tried to put the alias definition / sourcing of .bashrc in .bash_profile and also in .ssh/rc. But nothing of this works.
Note that I am not able to change how the ssh command is invoked since this is a part of some binary installation script. The only thing I can modify is the environment. Is there any other possibility to get this alias sourced when the ssh command is executed? Is there some ssh configuration which has to be adapted?
From the man pages of bash:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt
There are a couple ways to do this, but the simplest is to just add the following line to your .bashrc file:
shopt -s expand_aliases
Instead of:
ssh user#host "bash -c ll"
try:
ssh user#host "bash -ic ll"
to force bash to use an "interactive shell".
EDIT:
As pointed out here about non-interactive shells..
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# execution returns after this line
Now, for every alias in your bashrc file say i have:
alias ll="ls -l"
alias cls="clear;ls"
Create a file named after that alias say for ll:
user#host$ vi ssh_aliases/ll
#inside ll,write
ls -l
user#host$ chmod a+x ll
Now edit .bashrc to include:
# If not running interactively, don't do anything
[ -z "$PS1" ] && export $PATH=$PATH:~/ssh_aliases
This does the job.. although I am not sure if it is the best way to do so
EDIT(2)
You only need to do this for aliases, other commands in bashrc will be executed as pointed out by David "you must have executable for ssh to run commands".
an alternative to alias that will be visible in all script is
EXPORT & EXECUTE VARIABLE
# shortcut to set enviroment to insensitive case
export go_I="shopt -s nocasematch"
Now in any script you can use
#!/bin/bash
$go_I # go Insensitive
[[ a == A ]] # evaluates TRUE ( $? == 0)
$go_C # maibe want to go back to casesensitive
it's useful to place all shortcuts/aliases in /path/to/my_commands and edit /etc/bash.bashrc
source /path/to/my_commands
Open file ~/.bash_profile. If this file does not exist create one in the home directory and add the below line
source = $HOME/.bashrc
exit your ssh and login agian and you should get the .bashrc settings working for you.

Resources