Below you will find pages that utilize the taxonomy term “bash”
July 19, 2017
Process Substitution - Missing File Descriptors
I work with many servers each day, and more often than not I wind up sourcing a script or two from one of my remote servers, thusly:
. <(curl -Ss https://mygitrepo.whatever/repo/file.sh) Now and then the server complains by responding:
bash: /dev/fd/63: No such file or directory I went googling and found that actually it’s not a very uncommon thing to have happen. It turns out the symlink /dev/fd > /proc/self/fd was missing.
April 25, 2017
tr - translate or delete characters
purpose the tr command is for “translating” (replacing or deleting) characters in a string. Accepts input from stdin, and puts output on stdout. I am bold about characters because it’s not got a way to specify that you want to replace one string with another string (use sed for that).
examples single character replacement The basic usage on tr is to just replace one character with another. It’s case-sensitive:
echo "aAbBcCdD1234" | tr 1 R aAbBcCdDR234 #only the '1' was replaced with 'R' character set replacement One can specify a set of characters to be replaced by a set of others:
December 10, 2016
Why my PS1 sucked and what I did about it.
I always had problems with my PS1 variable. It would show up like this (in green):
username@boxname:[~/Documents] $ The idea was to have something bright to easily see the prompts while scrolling back up, and drop down a new line so I could allow the directory path to get as long as needed without reducing the amount of space I had to type in. Though the PROBLEM I had with it was that it prevented me from properly editing a command retrieved from history by pushing the up arrow on the keyboard.
December 9, 2016
Get rid of prefix/suffix bash
Sometimes a dude just needs to get rid of a prefix or a suffix:
string=some_database-file.sql suffix=.sql stuff=${string%$suffix} echo $stuff get rid of prefix using ‘#’: ${string#$prefix}
get rid of suffix using ‘%’: ${string%$prefix}
June 29, 2016
piping and redirection
xargs – takes results from one command and uses them as arguments to another command.
<command1> | xargs <command2> tee – takes stdin and prints to file and prints to screen. Thus to be able to capture and see output
<command1> | tee <filename> Redirection
<command> > file.txt 2>&1 This sends stdin and stdout’s output to file.txt
June 27, 2016
stdin, stdout and stderr
Redirecting
stdin is a channel where one provides input (keyboard, typically)
stdout is a channel that a script or program uses to output information
stderr is another channel used by a script or program to output errors
< redirect stdin
redirect stdout
2> redirect stderr
Examples:
This redirects any errors to the ‘err’ file
ls nonexistentfile 2> err This redirects any actual output to the ‘out’ file and errors to ‘err’
June 16, 2016
Environment variables
The only thing I really need to know is that this command displays all of them from the current session:
env<del></del> But I guess I’ll write down some other stuff too.
The ‘export‘ command makes a variable available to any children of the current session. That’s the only real reason that it should be used. Otherwise you can prepend to the path for instance using:
PATH=/home/myself/bin/:$PATH You can unset variables using the ‘unset‘ command
June 16, 2016
BASH one-line if
I recently learned about this:
(( 1 )) && echo "true" || echo "false" Which is a great way to do an if statement on a single line, if you ask me. I could just do if [[ cond ]]; do something else; another thing; fi but I think this one is pretty slick.
[[ cond ]] && <what happens if true> || <what happens if false>
June 16, 2016
BASH step values
This is cool, I can use this syntax:
{<start>..<end>..<increment>} To step through numbers! Like this:
$ echo {1..10..2} 1 3 5 7 9 It works in loops, or any old thing! Awesome!
June 14, 2016
Escaping characters in echo
I knew of this option:
echo -e "\042asdf\042" # this prints ("asdf") but never realized that one could also:
echo $'\042asdf\042' # this also prints ("asdf") I don’t even know what to call that…
June 1, 2016
Bash variables
So apparently $variable is actually a simplified form of ${variable}. Now I know! http://www.tldp.org/LDP/abs/html/varsubn.html
I’m considering making that my standard code, if that’s how they’re supposed to be…