The ~/.bash_history
file is only updated when you close/exit from the terminal. While you are in the terminal you can browse backwards in commands which was entered in that session. So if you exit from that terminal session your history will be updated with the missing command.
Also if you put a space at the beginning of a line, before the command, that command won't be saved in the history. This is a good practice if you issue a command which you don't want to be included in the history.
grep
does that job well enough.
$ echo "::=BEGIN" > testfile1.txt
$ grep "::=BEGIN" -q testfile1.txt && echo "FOUND"
FOUND
$ grep "::=BEGIN" -q testfile1.txt && echo "FOUND" || echo "NOTFOUND"
FOUND
$ grep "whever" -q testfile1.txt && echo "FOUND" || echo "NOTFOUND"
NOTFOUND
What the code does is simply running a quiet search within subshell. &&
, or logical AND operand, checks if the first command succeeded, and if it did, then it runs echo "FOUND"
. ||
checks if that echo has been run, i.e., if we found anything. Second echo runs only if the first command failed.
Here's an awk
version:
$ awk '{i="NOTFOUND";if($0~/::=BEGIN/) i="FOUNDIT";exit } END { print i}' testfile1.txt
FOUNDIT
$ awk '{i="NOTFOUND";if($0~/whatevs/) i="FOUNDIT";exit } END { print i}' testfile1.txt
NOTFOUND
Basic idea here is to set i
to NOTFOUND
, and if we find the string - change it to FOUNDIT
. At the end after the first set of has finished processing file, we will print i
, and it will tell us if we found something or not.
Edit: In the comment you mentioned that you want to place the result into a variable. Shell already provides a variable that reports exit status of the previous command, $0
. Exit status 0 means success, everything else - fail. For instance,
$ grep "::=BEGIN" -q testfile1.txt
$ echo $?
0
If you want to use exit status in a variable, you can do it like so : MYVAR=$(echo $?)
. Remember , $? report exit status of previous command. So you must use this right after the grep command.
If you want to still use my earlier awk and grep one-liners, you can use same trick MYVAR=$()
and place whatever command you want between the braces. The output will get assigned to variable MYVAR
.
Best Answer
Yes, spacing between words and redirections doesn't matter. That's the short answer.
The details lie in the fact that shell ( for simplicity let's just refer to
bash
only) treates certain characters and groups of characters as "words" and "metacharacters". From bash 4.3 manual:And
So when we do:
or
that's still 3 words ("hello world" can be considered a single shell word because it's quoted), with one
>
meta character and couple spaces. Shell will see it, and perform redirection first (so it looks for meta characters first), and then runs commands in accordance with its standard behavior.Order of redirections, however, matters a lot, especially when you're duplicating file descriptors with something like
2>&1
. Say you want to get send both stderr and stdin to same location. Here's a wrong way to do it:You're making file descriptor
2
output to same location as1
, which is your terminal, but it was already doing so. That's why stderr shows up.If you redirect
stdout
first, and only then change where2
points - then it'll work: