Shell Note
Content of this should be considered always under zsh
, unless otherwise specified.
coding first bash to convert obsidian notes#
- @2021-11-25
- manual Bash Guide for Beginners also in programming-docs
- How to check if a command succeeded?
:
is the placeholder like pythonpass
Bash syntax error when "else" follows an empty "then" clause- [ ] if syntax: why
if <exp>
andif [flag-exp]
? - [ ] if syntax: why is the last space in
if [[ ]] ;
needed? - There's a thing called Parameter Expansion like vscode textmate used
- Use string variable as path: the
'
and"
will effect the string parser, like python f-string. Don't use any delimiter on the definition. Do use quote variable de-references:echo "$games"
when quoting from How to add path with space in Bash variable - [ ] what is
$()
and()
.$(cd <path> && pwd)
won't work but without$
it works. derived from:Temporarily change current working directory in bash to run a command - open chrome with terminal
/usr/bin/open -a "/Applications/Google Chrome.app" 'http://google.com/'
from How can I open a URL in Google Chrome from the terminal in OS X?
glob filename pattern match#
- @2021-11-26
- objective is to delete all folder/files expect those under folder
res/
(background: MkDocs needs the resources all underdoc_dir
) - This is called glob or globbing in tldp and Wiki: glob. Here are some examples
!
not working in zsh, use^
instead:[!]
(exclamation mark in brackets) wildcard in bashman find
shows that the regex seems bounded with^
and$
.- Running a executable file in Mac 12.0 will actually use
sh
, checked withps -p $$
Mac flavor zsh#
- @2021-11-28
zsh
is not shell. When executingread -q
, shell will throw an error while zshell has a builtin flag to read only one key and parse to"y"
or"n"
-
to check builtin functions like
which
, we can useshell unalias run-help autoload run-help bindkey "^[h" run-help # input `type «Alt-h»' to run-help on type, but it's
TCC protection#
- (@2021-11-28) Mac
zsh
has a special constraint shown as Can't open input file: zshIn macOS Transparency Consent and Control (TCC) restricts access to "personal" data, including anything in a user's Desktop folder and outputs a /bin/zsh: can't open input file error message if you try to disobey this rule. Try moving file-name.zsh to a different folder.
- (@2021-11-28) The exact TCC protected folders are listed here MacOS Catalina launchd can't open input file error
These specific folders are TCC protected in Catalina: ~/Desktop, ~/Documents, ~/Downloads, iCloud Drive, third-party cloud storage, if used, removable volumes, network volumes.
- (@2021-12-01) The TCC will not block
source some-script.zsh
nor these files(also.sh
,.bash
) to run directly.
functions#
- @2021-12-01
- calling a function requires none
()
in mac terminal, but in VSCode terminal on Mac, the()
is accepted. - failing will be silent by default.
- [ ] how to change this? Test: Use
set -o errexit
to make it throw error.set -o pipefail
to make it throw error if any command in pipe fails. also testsetopt ERR_EXIT
(setopt ERR_EXIT=true
) (suggested by Copilot)
- [ ] how to change this? Test: Use
shell package managing system#
- @2021-12-04
- AKA Package Tool
- Advanced Packaging Tool
- On Mac,
brew
,pkgutil
are also used.
Permission to execute#
- @2021-12-15
chmod +x ${FILE_NAME}
Command line calculator#
- @2021-12-15
bc
is "An arbitrary precision calculator language"- Short guide
symbolic link#
- @2021-12
1 |
|
current directory#
- @2022-01-26
mydir=${0:a:h}
,${0}
is the file path. More details: Portable way to get script's absolute path?- @2022-01-17
- [wrong] The
$PWD
in zsh is the directory of the current runtime shell ~~file~~. - Bash script absolute path with OS X