Prevent bash adding single quotes [duplicate] - bash

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Convert a string into an array with bash, honoring quotes for grouping [duplicate]
(3 answers)
Bash doesn't parse quotes when converting a string to arguments
(5 answers)
Closed last year.
I am trying to execute a command like this
./build -t -c cflag="-Os -march=haswell"
The following is my script:
#!/bin/bash
set -x
ARGS=" -t"
MARCH=${MARCH:-haswell}
CFLAG="-Os"
CFLAG+=" -march=$MARCH"
ARGS+=" -c cflag=\"${CFLAG}\""
./build $ARGS
Here is build:
#!/usr/bin/env python3
import sys
for arg in sys.argv:
print(arg)
Bash always adding extra single quotes in the command:
+ ARGS=' -t'
+ MARCH=haswell
+ CFLAG=-Os
+ CFLAG+=' -march=haswell'
+ ARGS+=' -c cflag="-Os -march=haswell"'
+ ./build -t -c 'cflag="-Os' '-march=haswell"'
./build
-t
-c
cflag="-Os
-march=haswell"
Is there a way to disable this behavior in bash?

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.

bash variable expansion in for loop sub-command [duplicate]

This question already has answers here:
How do I use variables in single quoted strings?
(8 answers)
Closed 4 years ago.
Trying to expand a for loop variable in this does not succeed -
I am trying to use the $i variable in the jsonpath for loop below:
for i in {0..9}; do
echo $i
kubectl exec -i -t "$(kubectl get pod -l "app=mdm-shard" -o jsonpath='{.items[{$i}].metadata.name}')" -- cat /proc/net/udp
done
I get:
0
error: error parsing jsonpath {.items[{$i}].metadata.name}, invalid array index {$i}
error: pod name must be specified
I tried a lot of combinations but can't find the one that is going to expand $i inside the query.
My bash version:
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Thank you Benjamin - yes this worked:
for i in {0..9}; do
echo $i
kubectl exec -i -t "$(kubectl get pod -l "app=mdm-shard" -o jsonpath="{.items[$i].metadata.name}")" -- cat /proc/net/udp;
done

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'

How to pass a quoted argument to another program in a bash script? [duplicate]

This question already has answers here:
How to pass quoted arguments from variable to bash script [duplicate]
(1 answer)
How to pass quoted parameters to another program
(2 answers)
Closed 5 years ago.
rclone is a backup program I run from a bash script.
I want to pass in this parameter to rclone:
--exclude "{secret1b,secret1c}/**"
rclone needs those quotes.
Here is my failed attempt:
#!/bin/bash
cmd='rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**"'
printf "command: \n$cmd\n\n"
echo "$cmd"
#echo and printf output as expected:
#rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**"
#rclone with --exclude works as expected:
$(rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**")
#on these $cmd, rclone works but the --exclude is not excluding:
$cmd
$($cmd)
It's weird because the literal string output by echo "$cmd" works as expected.
$(rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**")
But the command $cmd does not work as expected.
$cmd
There are two similar questions that are NOT trying to pass in the quotes:
How to pass quoted arguments from variable to bash script
How to pass quoted parameters to another program

Resources