Strings are typically delimited by quotes, which raises the problem of dealing with strings that include quotes.

learn more… | top users | synonyms

61
votes
3answers
11k views

$VAR vs ${VAR} and to quote or not to quote

I can write VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}" the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
3
votes
4answers
208 views

How to escape shell metacharacters automatically with `find` command?

I've got a bunch of XML files under a directory tree which I would like to move to corresponding folders with the same name within that same directory tree. Here is sample structure (in shell): ...
5
votes
2answers
71 views

How to store a path built with wildcards and containing with spaces into a variable

Here's the situation (I'm on a Mac, OS X El Capitan): # This works: $ cd /Applications/Adobe\ Illustrator*/Cool\ Extras.localized/en_US/Templates/; # These do not work: $ ...
2
votes
1answer
52 views

Why are these nested options in a POSIX shell script being word-split?

I have a script count_args that counts arguments: #!/bin/sh echo $# If I call it with these options, it tells me it gets two arguments: $ ./count_args --options '1 2' 2 But if I construct the ...
18
votes
4answers
33k views

Pass shell variable as a /pattern/ to awk

Having the following in one of my shell functions: function _process () { awk -v l="$line" ' BEGIN {p=0} /'"$1"'/ {p=1} END{ if(p) print l >> "outfile.txt" } ' } , so when called as ...
20
votes
2answers
16k views

How are parentheses interpreted at the command line?

While reading up on how to set up grub, I came across an article claiming that I need to use one of the following two syntaxes, echo \(hd0,0\) >> /boot/grub/grub.conf or echo '(hd0,0)' ...
0
votes
2answers
307 views

Double quotes in bash

I am trying to create a bash file which calls a program that expects a parameter to submitted between two double quotes. Take a look at my file content. #!/bin/bash echo ./yowsup/yowsup-cli demos ...
0
votes
2answers
92 views

Echo a string with a variable in it without expanding/evaluating it [closed]

I've searched everywhere. Tried echo and print. Tried single and double quotes. But I have parsed data and assigned it to a variable and would like to then evaluate it for if there is a variable ...
3
votes
2answers
198 views

How does the -d option to bash read work?

I have a bash script where I'm trying to assign a heredoc string to a variable using read, and it only works if I use read with the -d '' option, i.e. read -d '' <variable> script block ...
0
votes
0answers
32 views

Is it possible to ssh and execute a remote command with a single quote in it? [duplicate]

ssh user@host "mv \"/some/path/fi\'le.txt\" /some/new/path" It doesn't matter what I do, or what combination of quotes I use on the file, it gives the following: bash: -c: line 0: unexpected EOF ...
1
vote
1answer
39 views

cshell alias: How to use nested single quotes(') in in cshell alias

I am trying below alias in cshell: alias sll 'ls -l \!* | grep -oE '[^ ]+$' | paste -s | xargs ls -l' For this CSH says, Illegal variable name. If I use \$, alias will be set without any error. ...
5
votes
2answers
118 views

How to insert newlines into PATTERN when using fgrep / grep -F / grep --fixed-strings

When using fgrep, the man page says it will Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. In bash, what's the correct way to insert ...
0
votes
2answers
32 views

Bash script doesn't convert variables [duplicate]

I was attempting to create a bash script to run a backup of my Raspberry Pi to my Box account via FTP. Most of it works, but for some reason it won't convert variables to the text they stand for. When ...
0
votes
1answer
44 views

Parenthesis on echo shell command

I have this echo command: $ echo "alias notepad++="C:/Program\ Files\ "(x86)"/Notepad++/Notepad++.exe"" >> ~/.bashrc As shown in the picture it does not recognize the parenthesis.
1
vote
2answers
118 views

How to use single quotes inside ssh and sqlplus

Below script helps me to ssh and do sqlplus to fetch info from database. I checked and it worked well but the only problem is if i use single quotes in the sql query then I am getting an error since I ...
38
votes
3answers
19k views

How can I execute `date` inside of a cron tab job?

I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use: 0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log ...
0
votes
0answers
16 views

Bash variable containing JSON splits on single quotes [duplicate]

(Apologies for the poor title, I'm not sure what the exact cause of my problem is which makes it hard to describe, I just know I can't get it to work.) I have a variable which contains JSON. I'm ...
1
vote
2answers
89 views

bash -c in subshell gives EOF parsing error

Why does the following script work as expected (prints hello) #!/bin/bash foo=$(bash -c 'echo hello') echo $foo while this script: #!/bin/bash cmd="bash -c 'echo hello'" foo=$($cmd) echo $foo ...
16
votes
6answers
576 views

Should variables be quoted when executed?

The general rule in shell scripting is that variables should always be quoted unless there is a compelling reason not to. For more details than you probably want to know, have a look at this great ...
3
votes
2answers
207 views

Passing paths with spaces as arguments

I am having difficulty in passing some string variables having spaces in them as arguments to a program. For debugging and showing the arguments being passed, I created a demo Python script -: ...
1
vote
1answer
48 views

Run for loop via ssh

Good day All, I'm attempting to run a for loop via SSH. The loop is used to get the last login of each user that has an account on the server, ssh 172.20.2.1 '''for i in `cat /etc/passwd | awk -F: ...
1
vote
1answer
55 views

find command match issue [duplicate]

I'm having some trouble while searching files with find command and the way it handles the search. Let's say I'm currently in the directory /tmp and the directory contains the files: ...
43
votes
3answers
15k views

using single or double bracket - bash

I'm confused with using single or double bracket. look at code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains space. ...
0
votes
2answers
4k views

variables in find command and more shell problem [duplicate]

Possible Duplicate: Recursive rename files and directories I wrote the following script: #!/bin/bash SAVEIFS=$IFS alias export='export' IFS=$(echo -en "\n\b") find $1 -name "*" ...
2
votes
5answers
42 views

Help on edit a sed statement

I'm writing a script to change on the fly the firefox home page through a text variable: sed -i 's|\("browser.startup.homepage",\) "\(.*\)"|\1 "$ddrs"|' /home/max/.mozilla/firefox/*.default/prefs.js ...
3
votes
2answers
147 views

Shell-independent escaping for strings in scripts

I just struggled (again) over this: # only in bash NORM=$'\033[0m' NORM=$'\e[0m' # only in dash NORM='\033[0m' # only in bash and busybox NORM=$(echo -en '\033[0m') The goal is to include special ...
2
votes
2answers
60 views

Exported variables inside ssh session are empty

I am trying to export a variable inside a ssh session and reference the variable in the next write command. sshpass -p "password" ssh -t -t my-box <<EOF export newUrl="this is a url" sudo ...
2
votes
2answers
200 views

Passing multiple of arguments with whitespaces through a script to ssh

I want to remove multiple files from remote server. I have all files under one array in a script and I call other script which will remove files. Let output of "${b[@]}" is: ...
2
votes
1answer
37 views

quotes generated by another Bash script not treated like quotes inside original script [duplicate]

Using bash… my pwd contains directories with spaces: $ pwd /a/b c/d This works: $ ls "$PWD" bar foo # yay! It also works if I put that code inside a script. Now if I split that command in two: ...
5
votes
3answers
659 views

File name beginning with - (dash) [duplicate]

I have file called -l in my directory now I tried to do for i in *; do stat -c "%s %n" "$i"; done it lists all files with sizes but in the middle of the output there is something like 395 ...
0
votes
1answer
34 views

Script with variable in path

I'm not entirely sure where to ask or how to formulate this question with correct terminology. I have script here for managing a game (minecraft) server for the user (bukkit). I've cut out some ...
1
vote
2answers
227 views

Remove file with pound signs around it [duplicate]

I am using SSH to another terminal with very limited disk space. upon typing ls I see a file called #korpus.txt#. I suspect this file is an emacs buffer, but upon running emacs and trying to kill it ...
1
vote
1answer
77 views

How to declare some aliases dynamically in zsh?

I would like to declare a few shortcut commands to switch between my various coding projects, so I have come up with the following script. projects=$(ls -d ~/Workspace/*/) ...
1
vote
1answer
28 views

Why isn't this perl regex capturing the backreference?

I have a directory of files $ ls -1 ep2.mp4 ep3.mp4 ep4.mp4 ep5.mp4 ep6.mp4 ep7.mp4 I want to use the perl rename utility to make all files look like e.g. ep02.mp4 Command I am using rename -n ...
1
vote
1answer
80 views

Inserting variables in a command within a shell script

I'm attempting to write an automated script which will update the IP address for my Raspberry Pi within my /etc/hosts file. I'm able to execute this line just fine, IP=`sudo nmap -sP 10.61.4.0/24 | ...
0
votes
1answer
37 views

Escape shell arg from one script to another

Given ./mysh0: #!/bin/bash exec ./mysh1 $* And ./mysh1: #!/bin/bash echo $1 echo $2 echo $3 How do I call mysh0 such that the arguments to mysh1 and what's eventually printed are "A", "B 2" ...
2
votes
1answer
19 views

Parameter ignored when quoted

I have a directory with 3 files: file1, file2 and the following bash script: #!/bin/bash set -e Command="ls -1 -I file1" echo "Command: $Command" Files=`$Command` echo "Files:" echo $Files The ...
8
votes
1answer
223 views

Bash script parsing argument with quotes

Please consider this simple script named test: #!/bin/bash echo $1 and the following commands and calls: $ echo "A B" A B $ echo ""A B"" A B $ echo """A B""" A B $ echo """A B""" A B $ ./test "A ...
21
votes
3answers
2k views

gnu find and masking the {} for some shells - which?

The man page for gnu find states: -exec command ; [...] The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, ...
2
votes
2answers
174 views

Do programs invoked by shell inherit shell variables?

If I have this shell script str="A tremor in the Force. The last time I felt it was in the presence of my old master." cat <<< "$str" My understanding is that the command cat ...
0
votes
1answer
68 views

escaping brackets and other characters in shell script

I have run the following command on the command line and it works fine, but when I try running it from a shell script and run shell script from the terminal I get the following error: join -t $'\t' ...
1
vote
2answers
90 views

Escape characters from echo -e

I have the following in my .bashrc file I use for a log: function log(){ RED="\e[0;31m" RESET="\e[0m" echo -e "${RED}$(date)" "${RESET}$*" >> "$HOME"/mylog.txt } But when I do ...
0
votes
3answers
61 views

significance behind escaping parentheses in expr match

Can anybody give me the difference between the following two commands in the context of escaping parentheses, or refer some document that would clear my doubt. string=abcABC123ABCabc $ echo `expr ...
0
votes
2answers
61 views

How to write “!” symbol between double quotes in bash? [duplicate]

I can't figure out how to write ! symbol in bash scripts when putting it in double quotes strings. For example: var="hello! my name is $name! bye!" Something crazy happens: $ age=20 $ name='boda' ...
2
votes
2answers
144 views

Insert text in specific lines of a file? with special characters

i know this has been answered several times, but i can't figure out how to use it for my project. i have several php files and want to add a new string between line 11 and 12. this is the string i ...
2
votes
1answer
86 views

Error when substituting part of curl command with variable

I'm writing a script where I am running curl commands several times. The use of the curl command varies throughout the script and I would like to save myself time (in the future when I have to debug ...
1
vote
1answer
56 views

What does a backslash represent when searching for decimals

I want to understand what the backslash represents in this command. grep "\.900983" table I know what the command does, it searches for the 900983 value in table, I'm just not sure of the purpose of ...
17
votes
4answers
31k views

How to escape quotes in shell?

I'm having trouble with escaping characters in bash. I'd like to escape single and double quotes while running a command under a different user. For the purposes of this question let's say I want to ...
0
votes
1answer
159 views

Passing a variable argument to egrep in a bash script

I have a script, myScript, which is trying to egrep the script argument in a file. Somehow variable expansion isn't working properly with the egrep command. I believe I've isolated the problem ...
2
votes
2answers
173 views

How to escape single or double quotes when passing a variable to bash script?

Let;s say I have a script which echoes $1 #!/bin/bash echo $1 It's called test.sh. Then I call /bin/test.sh 'test'. The output is test. But this doesnt work: /bin/test.sh 'te'st' There's a ...