Linked Questions

1
vote
2answers
14k views

if statement with grep [duplicate]

What I'm trying to do is the get the user input and search through the file, If it match it will show the result, else it will echo $keyword not found. But my scrip is always return fault. ...
1
vote
2answers
663 views

Bash: Escaping double quotes in $() command substitution [duplicate]

I am having trouble wrapping my head around how command substitution works when part of the command's parameters come from a variable. To illustrate, I'll just present a series of commands: I'll ...
0
votes
1answer
59 views

How to get bash not to treat [ as a test character when using command substitution? [duplicate]

How can I get unmodified output from $() in bash? I need to have bash return the results of command substitution (via $(command) or `command`), but bash treats the left square bracket as the test ...
0
votes
1answer
21 views

dealing with nested quotes in bash scripts redux [duplicate]

I want to construct a bash script which contains nested double quotes. I've tried all of the usual tricks, in particular rprint which everybody recommends. But nothing I've tried works. In ...
159
votes
4answers
61k 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?
133
votes
1answer
50k views

When is double-quoting necessary?

The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
39
votes
2answers
8k views

Are quotes needed for local variable assignment?

Can I safely omit quotes on the right side of a local assignment? function foo { local myvar=${bar} stuff() } I'm mainly interested in bash, but any info on corner cases in other shells are ...
10
votes
1answer
10k views

understanding the default value of IFS

In my GNU bash version 4.2.8, IFS has a default value of space, tab and line feed by default: usr@T42 ~ $ echo -n "$IFS" | hexdump -C 00000000 20 09 0a | ..| ...
6
votes
2answers
198 views

Do I need to quote command substitutions when assigning their output to a variable? [duplicate]

I tend to quote command substitutions as shown below even when assigning their output to a variable: var="$(command)" Is that actually needed though? When does it break? The accepted answer ...
-1
votes
4answers
211 views

Bash: how to build JSON from multiline strings [closed]

My awk outputs a multiline string. awkresult=`awk '{...}'` echo "{ \"result\": \"$awkresult\" }" > result.json The multilines must be preserved in the result string. I think the best way is to ...
2
votes
2answers
174 views

Mixing local and remote variables and proper usage and quotation

The recommended pattern for bash as far as I know is to always quote the shell variables. E.g. "$VAR" instead of $VAR. But how can I achieve the same safety the quotes provide for variables meant to ...
2
votes
1answer
167 views

Wordsplitting occurring in quoted variable

I'm trying to create a dialog menu based on the results of the lsblk command. My goal is to read the first word as the menu item and the remaining words as the menu item description. For example: ...
1
vote
1answer
63 views

Sed command to delete lines - variable [duplicate]

Using sed command I want to delete every other three lines from a file which has e.g. 99 lines in total. I mean to keep the first three lines and delete the second three lines and so on. My starting ...
1
vote
1answer
72 views

Ambigious Redirect when using alias to export environment variable of path with spaces?

I'm trying to simplify switching between KANBANFILE's using kanban.bash on Windows. I'm using several aliases such as the following for switching between KANBANFILEs: alias k.pos="export KANBANFILE=\...
0
votes
1answer
87 views

Add folder name ($date) to top of log then cut line 21 from same log and add as a value in running script

I am trying to create a script and I am stuck on the final part I want to add. The script syncs some paths on my server to my google drive. If there are any deleted or modified files on my server it ...