Extract filename and extension in Bash

First, get file name without the path: filename=$(basename — “$fullfile”) extension=”${filename##*.}” filename=”${filename%.*}” Alternatively, you can focus on the last “https://stackoverflow.com/” of the path instead of the ‘.’ which should work even if you have unpredictable file extensions: filename=”${fullfile##*/}” You may want to check the documentation : On the web at section “3.5.3 Shell Parameter Expansion” … Read more

Can you pass an environment variables into kubectl exec without bash -c?

/usr/bin/env exports values passed in key=value pairs into the environment of any program it’s used to invoke. kubectl -n nmspc exec “$POD” — env curIP=123 script01 Note that you should never use $runScript or any other unquoted expansion to invoke a shell command. See BashFAQ #50 — I’m trying to put a command in a … Read more

zip columns from separate files together in bash

NAME paste — merge corresponding or subsequent lines of files SYNOPSIS paste [-s] [-d list] file … DESCRIPTION The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file’s newline characters with a single tab character, and writes the resulting lines to standard output.

Achieve Local Function

Bash does not support local functions, but depending on your specific script and architecture you can control the scope of your function name through subshells. By replacing the {..} with (..) in your definition, you’ll get the output you want. The new definition of usage will be limited to the function, but so will e.g. … Read more

Declaring global variable inside a function

declare inside a function doesn’t work as expected. I needed read-only global variables declared in a function. I tried this inside a function but it didn’t work: declare -r MY_VAR=1 But this didn’t work. Using the readonly command did: func() { readonly MY_VAR=1 } func echo $MY_VAR MY_VAR=2 This will print 1 and give the … Read more