How to get shell to self-detect using zsh or bash

If the shell is Zsh, the variable $ZSH_VERSION is defined. Likewise for Bash and $BASH_VERSION. if [ -n “$ZSH_VERSION” ]; then # assume Zsh elif [ -n “$BASH_VERSION” ]; then # assume Bash else # assume something else fi However, these variables only tell you which shell is being used to run the above code. … Read more

Replacement for source in sh

The dot command ‘.‘ is the equivalent of the C Shell (and Bash) source command. It is specified by POSIX (see dot), and supported by the Bourne and Korn shells (and zsh, I believe). . somefile Note that the shell looks for the file using $PATH, but the file only has to be readable, not … Read more

How to get script directory in POSIX sh?

The POSIX-shell (sh) counterpart of $BASH_SOURCE is $0. see bottom for background info Caveat: The crucial difference is that if your script is being sourced (loaded into the current shell with .), the snippets below will not work properly. explanation further below Note that I’ve changed DIR to dir in the snippets below, because it’s … Read more

Are shell scripts sensitive to encoding and line endings?

Yes. Bash scripts are sensitive to line-endings, both in the script itself and in data it processes. They should have Unix-style line-endings, i.e., each line is terminated with a Line Feed character (decimal 10, hex 0A in ASCII). DOS/Windows line endings in the script With Windows or DOS-style line endings , each line is terminated … Read more

Shell script current directory?

As already mentioned, the location will be where the script was called from. If you wish to have the script reference it’s installed location, it’s quite simple. Below is a snippet that will print the PWD and the installed directory: #!/bin/bash echo “Script executed from: ${PWD}” BASEDIR=$(dirname $0) echo “Script location: ${BASEDIR}”

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)