shell

set -o xtrace for just one line?

set -o xtrace for just one line? bash 4.4.20(1) on RHEL 8.4. set -x ; ./blarge ; set +x works, of course, but it’s just that much more stuff to remember to remove when you’re finished. And gets sticky if you’re running ssh, sudo, etc. Thus, as mentioned in the Subject, is there some way …

Total answers: 2

How to store and source environment variables when they contain semicolon (`;`)

How to store and source environment variables when they contain semicolon (`;`) I’m trying to exchange environment variables and restore them quickly between terminals. I define two functions in my shell (~/.bashrc): save_env(){ mkdir -p ~/tmp printenv > ~/tmp/env } load_env(){ source ~/tmp/env } Both of them work as expected except when there is a …

Total answers: 1

Shell: How to print all files in a folder with cat with files separator

Shell: How to print all files in a folder with cat with files separator If I do cat * in a directory, then I cannot easily see when I switch from one file to the next. I would like to have a visual indication that the files are not the same. I tried options -n …

Total answers: 1

How to make bc handle explicitly positive numbers?

How to make bc handle explicitly positive numbers? Consider the following: $ echo "-1" | bc -1 $ echo "(1)" | bc 1 $ echo "+1" | bc (standard_in) 1: syntax error Minus sign and round braces are handled correctly but when explicit plus sign is specified bc falls with error. Is it possible to …

Total answers: 2

Relaunch a terminal using a terminal command

Relaunch a terminal using a terminal command Today, to convert from Markdown to PDF using Pandoc, it was necessary to install pdflatex. I did brew install basictex and then it was necessary to relaunch Terminal. For this, I simply closed it and opened it again. But is it possible to relaunch it using a command, …

Total answers: 1

How to specify several alternative conditions (OR operator) for single case statement? (Or Alternatively, where is shell case syntax description?)

How to specify several alternative conditions (OR operator) for single case statement? (Or Alternatively, where is shell case syntax description?) I want something like this: time="1m" case "$time" in *h*) *m*) *s*) echo "handling several cases for h, m, s tokens" ;; *) echo "handling other cases" ;; esac How to achieve this for POSIX …

Total answers: 1

How can I replace the last occurrence of " before ] (text-manipulation)

How can I replace the last occurrence of " before ] (text-manipulation) I’m doing some text manipulation and I want to replace the last occurrence of " (with ", "four") before ] which could be in different lines. (this essentially adds a new item in the array) e.g: array = ["one", "two", "three"] other_array = …

Total answers: 5

How can I add a property in the end of the object (text manipulation)

How can I add a property in the end of the object (text manipulation) I’m doing some text manipulation and I want to add a property (at the end) to an object named "object" that is inside a configuration file e.g . . object = { "one": "one", "two": "two", } . . Should become …

Total answers: 3

Behavior of truncating multiple slashes in paths stemming from glob pattern matching

Behavior of truncating multiple slashes in paths stemming from glob pattern matching From my limited research it seems that in bash, any multiple slashes after a glob pattern are truncated, like so: echo ////[h]ome////user////Desktop////test//// # outputs ////home/user/Desktop/test/ echo ////home////user////Desktop////tes[t]//// # outputs ////home////user////Desktop////test/ Is this conclusion correct? Is this behavior defined? If so, where? Is it …

Total answers: 1

Error in command su

Error in command su I am creating a command that will change a certain line in the /etc/profile file from a script, however in certain versions of Linux the sudo command may not be activated so it would be necessary to use the su command for this, within the script when executing: Inside the /etc/profile …

Total answers: 1

SIGTSTP(Ctrl-Z) not working for script started by script command

SIGTSTP(Ctrl-Z) not working for script started by script command Note: script refers to the script command, which logs stdin and¬†stdout to files. I was trying to run a script with the¬†script utility to save output for later inspection while preserving text colors on terminal. When I try to suspend the current script with Ctrl+Z (SIGTSTP), …

Total answers: 1

How to match a pattern instead of a single letter/number using brackets?

How to match a pattern instead of a single letter/number using brackets? I want exclude MSG, PDF and DOC from my path with shell parameter expension using brackets. When I place MSG between brackets only deletes the M instead of MSG. I looked around on the internet and read the documenation but still fail to …

Total answers: 3

How to replace both lower and uppercase extensions with Parameter Expansion?

How to replace both lower and uppercase extensions with Parameter Expansion? I’m converting doc files to txt using catdoc on Linux. To keep the same file name as output file I’m replacing the .doc extension with .txt using parameter expension. But there are many doc files ending on .DOC. How to make the .doc in …

Total answers: 2

Double backslash disappears when printed in a loop

Double backslash disappears when printed in a loop I have a script that joins together various lists of data fields which then needs to have a few more columns added. The file generated looks like this: $ cat compiled.csv "name":"Network Rule 1", "description":"Network Rule 1", "from":["internal"], "source":["any"], "user":["domain\network_user1"], "to":["external"], "destination":["host.example.com","10.1.2.1"], "port":["8443","22"], "name":"Network Rule 2", "description":"Network …

Total answers: 1

Where do I find documentation for the output of ls -l?

Where do I find documentation for the output of ls -l? There have been multiple questions asked about this, like Understanding ls output, What are columns in ls -la?, What does ‘ls -la’ do?, What do the fields in ls -al output mean?, etc.. I’ve also come across many other websites with articles attempting to …

Total answers: 2

Enabling command hashing in tcsh

Enabling command hashing in tcsh It seems command hashing is disabled by default in our tcsh environment, and I’m not permitted to get it enabled across the board. Instead I’m looking to enable command hashing within individual scripts, all of which contain while loops, so I’d expect the first iteration to loop through all the …

Total answers: 1

POSIX Shell: `exec` with changed arg0

POSIX Shell: `exec` with changed arg0 I want to exec a program and control it’s arguments including arg0 and environment. Using C I could go for execve. Can I do this in POSIX shell? Asked By: Patrick Böker || Source According to POSIX documentation of Special Builtin Utilities, the syntax for exec is exec [command …

Total answers: 1

How to source environment variable in command line with && combiner?

How to source environment variable in command line with && combiner? For a NodeJs app, I wrote the following script to deploy the app: "scripts": { "deploy": "source private/create_env.sh && ./deploy.sh", } private/create_env.sh creates environment variables that are used in the deploy.sh script. export USERNAME=alphonsio export SERVER=ftp.example.com export … The && combiner prevent the environment …

Total answers: 2

Force tcsh to check whether command exist in the path before attempting to execute it

Force tcsh to check whether command exist in the path before attempting to execute it I’ve noticed that tcsh, regardless of whether "-f" flag is passed on the shebang line, will iterate through $PATH, and try to execute the command from that path until the command is found. Whereas bash first checks whether the command …

Total answers: 1