Can I make "bash -x" not print the "+ " prefix? - bash

When I invoke bash with the -x option, it prints each command before it is executed. However - the commands are printed with a "+ " prefix.
Is there a way to get it to print the commands, but without this prefix? With -x or without it?

You can customize the prefix when using bash -x by setting the variable PS4. So in your .bashrc, you could have something like this:
export PS4=''
Or, if you want to debug something, let it print the line number like this:
export PS4='[ $LINENO ] '

Insert as the first line :
set -v
then run it without -x

Related

curl command works manually but not in script

I have a curl command in a script, when the script is run the command isn't able to fetch a resource (the command itself works, it's the response that's incorrect), but if I copy and paste the same command into the terminal I get the expected response.
After reading this my script looks like this:
jsess=`awk '/\sJSESSION/ { print "\x27"$6"="$7"\x27" }' cookies.txt`
ARGS=( -k -v -b $jsess $url7)
echo "curl ${ARGS[*]}"
curl "${ARGS[#]}"
and the last echo looks like this:
curl -k -v -b 'JSESSIONID=hexystuff' https://secretstuff.com
The last curl doesn't work, but copy-pasting that echo works. Any ideas what could be wrong? Thanks.
The problem seems in the two single quotes, try this :
jsess="$(awk '/\sJSESSION/ { print $6"="$7 }' cookies.txt)"
ARGS=( -k -v -b "$jsess" "$url7")
echo "curl ${ARGS[*]}"
curl "${ARGS[#]}"
args="-k -v -b"
jsess=$(awk '/\sJSESSION/ { print "\x27"$6"="$7"\x27" }' cookies.txt)
url7="https://secretstuff.com"
curl "${args}" "${jsess}" "${url7}"
The use of arrays is not my personal preference, and I believe the current situation demonstrates why. I believe that as much as possible, every individual item of data should be contained in its' own variable. This makes accessing said variables much simpler, and also greatly increases flexibility. I can choose exactly which pieces of information will go into a given command line.

Script to print pass arguments to script

I am using simple script to print the arguments. But not able to do so.
i am using cat command to add content to a file.
[root#cen06gst ~]# cat<<EOF>pass.sh
echo " you have passed me" $#
> EOF
But when i am seeing the file content again using cat , this is showing
[root#cen06gst ~]# cat pass.sh
echo " you have passed me"
Cat only is a command line tool to concatenate and print to the screen, it doesn't modify the file. Read
man cat
If you want to run your script, run
./pass.sh argument
It is also good practice to start your script with a shebang:
#!/bin/bash
Without it the system doesn't know what language to use to process the script.

Why does sh generate garbled output for set -x unexpectedly?

When I run shell script with -x option, I'll get more runtime debug information about the script, but sometimes this option will generate garbled output as bellowing,
$ sh -xc 'echo "中文"'
+ echo $'中?\226\207'
中文
But if I change a little bit this script, like adding a space at the beginning of the text,
$ sh -xc 'echo " 中文"'
+ echo ' 中文'
中文
the output is absolutely right again! And you will find out that the $ symbol disappears in the latter script. If I replace space to one of symbols #{}[]()&~^*\;<>|, I can also get similar right output. So what's happen to sh -x? Can anybody explain this to me ? Thanks in advance!

How to print Shell command literally?

In a shell script, is there a way to print command in this literal form after all variables are replaced with real value. For example, mkdir -p $HOME/src, before this command is executed, print something like mkdir -p /home/user/src. Thanks.
Try set -x argument and check this

BASH script to pass variables without substitution into new script

As part of a system build script I have a script that creates various files and configurations.
However one part of the build script creates a new script that contains variables that I don't want resolved when the build script runs. Code snippet example
cat - > /etc/profile.d/mymotd.sh <<EOF
hostname=`uname -n`
echo -e "Hostname is $hostname"
EOF
I have tried all sorts of combinations of ' and " and ( and [ but I cannot get the script to send the content without substituting the values and placing the substitutes in the new script rather than the original text.
Ideas?
The easiest method, assuming you don't want anything to be substituted in the here doc, is to put the EOF marker in quotes, like this:
cat - > /etc/profile.d/mymotd.sh <<'EOF'
hostname=`uname -n`
echo -e "Hostname is $hostname"
EOF
Easiest is to escape the $
echo -e "Hostname is \$hostname"

Resources