bash
Draw a horizontal line from dash character occupying the full width of a terminal in bash
Try with: echo some text printf ‘%*s\n’ “${COLUMNS:-$(tput cols)}” ” | tr ‘ ‘ – echo some text
In bash, how to get the current status of set -x?
You can check the value of $- to see the current options; if it contains an x, it was set. You can check like so: old_setting=${-//[^x]/} … if [[ -n “$old_setting” ]]; then set -x; else set +x; fi In case it’s not familiar to you: the ${} above is a Bash Substring Replacement, which … Read more
How can I grep complex strings in variables?
if echo “$long_str” | grep -q “$shrt_str”;then echo “found” fi or echo “$long_str” | grep -q “$shrt_str” && echo “found” || echo “not found” But since you are using bash shell, then use shell internals. No need to call external commands shrt_str=”guide” case “$long_str” in *”$shrt_str”* ) echo “Found”;; * ) echo “Not found”;; esac
How to display only different rows using diff (bash)
a.txt: 1;john;125;3 1;tom;56;2 2;jack;10;5 b.txt: 1;john;125;3 1;tom;58;2 2;jack;10;5 Use comm: comm -13 a.txt b.txt 1;tom;58;2 The command line options to comm are pretty straight-forward: -1 suppress column 1 (lines unique to FILE1) -2 suppress column 2 (lines unique to FILE2) -3 suppress column 3 (lines that appear in both files)
echo multiple lines into a file
cat >> /path/to/existingFile.text<< EOF some text line 1 some text line 2 some text line 3 EOF switch cat >> to cat > to create a file instead of append cat > /path/to/newFile.text<< EOF some text line 1 some text line 2 some text line 3 EOF
Downloading all the files in a directory with cURL
If you’re not bound to curl, you might want to use wget in recursive mode but restricting it to one level of recursion, try the following; wget –no-verbose –no-parent –recursive –level=1 \ –no-directories –user=login –password=pass ftp://ftp.myftpsite.com/ –no-parent : Do not ever ascend to the parent directory when retrieving recursively. –level=depth : Specify recursion maximum depth … Read more
How can I extract multiple 7z files in folder at once in Ubuntu?
7za -y x “*.7z” The above code worked for me
~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you. Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following: echo “source … Read more
How to assign a directory / path / folder with a space in the name to a Bash variable?
mount /dev/sda9 “$games” As mentioned, always quote variable dereferences. Otherwise, the shell confuses the spaces in the variable’s value as spaces separating multiple values.