How to remove the character before the pattern found? (Unix) - bash

I have a variable:
$var="-- ---comment- --
abcd;"
I want to remove every character that is between "--" comment "--"
Expected output:
$var="abcd;"
What I am trying in test.sh
#!/bin/bash
var="----comment---
abcd;"
test=$(sed 's/^--.*--/' $var)
echo $test
It is not working. I get the following error:
command not found

Trying to replace the pattern that you don't expect in output with nothing, something like this
test=$(echo $var | sed 's/^--.*--//g')
Should work.

In bash:
$ cat foo.sh
var="----comment---
abcd;"
echo "$var" # this outputs the original var
echo "${var/--* /}" # this output replacing everything between -- and " "
$ bash foo.sh
----comment---
abcd;
abcd;
You can set the output to a variable by
$ var="${var/--* /}"

Related

Double quotes in bash variable assignment and command substitution

I have a few questions about variable assignment and command substitution:
Why does \"<Enter> add a new line to the output
$ v1="1\"
2"
$ echo "$v1"
1"
2
?
Why
$ v2=$(echo -e "123\n\n\n")
$ echo "$v2"
123
while
$ v2=$(echo -e "123\n\n\n5")
$ echo "$v2"
123
5
?
How to correctly use quotes in such constructs:
v3="$(command "$v2")"
?
First question
< Enter > equal to new line, also equal to \n.
Use following code to explain:
function print_hex() {
HEXVAL=$(hexdump -e '"%X"' <<< "$1")
echo $HEXVAL
}
v1="
"
v2=$'\n'
print_hex $v1
print_hex $v2
---------output---------
A
A
In hex mode printing it is seen that v1 and v2 are equal.
Seconde question
echo manual explain link.
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
Third question
Do you mean print the string or mean get the command output?
The following example v3 is print the string and v4 is get the command output.
v2=.
v3="\$(ls \"$v2\")"
v4=$(ls "$v2")
echo $v3
echo $v4
---------output---------
$(ls ".")
test1.sh

Bash add characters before and after $foo

So if I have
$foo = foo
I would like to add : at the beginning and ! at the end. I tried the below:
bar=$( ":" $foo "!" )
You can't use bar=$( ":" $foo "!" ), with this command you try to assign the result of :foo! command to your variable. You will probably get an output like :foo!: command not found and your $bar variable will be not set.
You can use :
foo=foo
bar=':'$foo'!'
echo $bar
# Will return :foo!
the builtin printf can do that also but I'm not sure if it is portable solution.
foo=foo
printf -v bar ":$foo!"
echo "$bar"
see
help printf
or
man printf

assigning output to a variable using echo command

The below code does not give any output:
$echo `cat time`
19991213100942
$a=$(echo `cat time`) | echo $a | echo ${a:0:4}
Please tell where I am making mistake.
a=$(echo `cat time`)
assigns the output of the command inside the brackets $(...) to the variable $a.
Later in the script, you can print the variable:
echo $a
That prints: 19991213100942
echo ${a:0:4}
That prints: 1999
You can reference the varibale by its name $a.
First, you don't need to echo the output of cat time: just cat time.
Second, as #Etan says (kind of), replace the pipes with semicolons or newlines
a=$(< time) # a bash builtin, equivalent to but faster than: a=$(cat time)
echo $a
echo ${a:0:4}

How to print literal string "$1" in bash script?

I want to print string called "$1". But when I do this with echo it prints string which equals to "$1" variable. How can I print "$1" just like string?
for example:
set -- "output" # this sets $1 to be "output"
echo $1 # ==> output
But I want this:
echo $1 # ==> $1
You have to escape the $ to have it shown:
$ echo "\$1"
or, as noted by JeremyP in comments, just use single quotes so that the value of $1 does not get expanded:
$ echo '$1'
You need to either:
Enclose the variable in SINGLE quotes: echo '$1'
Escape the $ sign: echo "\$1"

Variable with '-' (minus signals) in Bash

This is so simple yet...
FOLDER='/home/user/.ssh'
SSH="$FOLDER/local-rsync-key.pub"
if [ -f "$SSH" ]; then
...
It looks that Bash considers the '-' as a minus signal and the IF statement always fails... How can I write this variable the right way?
UPDATE:
This is another real example:
I am tring to rename files with "-" in front of the filename, for example: "-0001.jpg"
However, everyime I try to run:
for i in *; do mv "$i" "${i//-/}"; done
or:
for i in *; do mv "$i" "${i#*-}"; done
I got this error:
mv: invalid option -- '0'
Try `mv --help' for more information.
Thanks for any light!
You should not have a $ in front of your SSH assignment, that's only needed when you're using the variable. Without that, it works fine, as in the following transcript:
pax> touch abc-xyz
pax> ll a*
-rw-r--r-- 1 pax paxgrp 0 2011-06-24 05:15 abc-xyz
pax> FOLDER=.
pax> $SSH="$FOLDER/abc-xyz"
bash: =./abc-xyz: No such file or directory
pax> SSH="$FOLDER/abc-xyz"
pax> if [ -f "$SSH" ]
...> then
...> echo yes
...> fi
yes
pax> _
The answer is to use "--" (indicating no more options) after "mv" or "./" before the name of the file (indicating it is about a file). For example:
for i in *; do mv -- "$i" "${i#*-}"; done
or:
for i in *; do mv -- "$i" "./${i#*-}"; done
In bash syntax, when you set a variable just use the variable name:
VAR=value
When you reference the variable, use the $ prefix:
echo $VAR
Your code has a stray dollar sign prefix where you are trying to set the SSH variable. The dashes inside the variable should be no problem.

Resources