Can Mutt be used to access Inbox messages in a bash script?

I’ve got scripts that collect data on errors and sends them into a ticketing system. If any developments occur regarding an existing ticket, I want to be able to access the email inbox to get the ticket number so that I can include this in the reply. This will result in the ticketing system including the new information in the old ticket instead of creating a new ticket every time. Is this possible?

I’ve been looking online for how I might be able to do this but I haven’t yet found anything that looks like bash commands to access the inbox programmatically and save a message’s information to a variable.

Go to Source
Author: Tom Cayton

GNU Date: inconsistency calculating number of days between dates

I am trying to calculate the number of days between two dates like this:

$ echo $((($(date +%s -d 2016/11/22)-$(date +%s -d 2016/11/20))/(3600*24))) days
2 days

That is the expected answer and perfectly consistent with this:

$ date -d '2016/11/22 - 2 days'
Sun Nov 20 00:00:00 CET 2016

However, these two seem to be inconsistent:

$ echo $((($(date +%s -d 2020/06/28)-$(date +%s -d 2016/11/20))/(3600*24))) days
1315 days

$ date -d '2020/06/28 - 1315 days'
Mon Nov 21 00:00:00 CET 2016

Am I missing something? Why don’t I get 1316 days (instead of 1315) in the third command I used?

I have done a few more tests changing the month in the date 2020/06/28. It seems that up to March I get the expected answer (i.e., I get Nov 20 in the fourth command), but from April on the inconsistency is present (i.e., I get Nov 21 in the fourth command). Any hints?

Go to Source
Author: orelleiro

How to obtain the path of a Bash script, when it’s executed through a symlink?

I want to obtain the path of a Bash script, is when it’s executed through a symlink.

In this case, $0 is -bash, while BASH_SOURCE is the symlink’s path.

Can I obtain the script’s path through some shell variable, built-in shell command, or external command?

Will BASH_SOURCE always holds the initial symlink’s path, even if the script is executed through several levels of indirection (multiple symlinks)?

Could I use ls with BASH_SOURCE to always retrieve the script’s path?

Go to Source
Author: Shuzheng

Return “continue” from function called from loop

I’m currently refactoring a script which has slowly grown beyond control. I’m trying to spin off repetition into functions. However, I have a repeated test being called from a loop, and want it to return continue.

Shellcheck says

SC2104: In functions, use `return` instead of `continue`.

And the shellcheck wiki says don’t do it. But is there a way?

Below is an example:


AFunction () {
    if [[ "${RED}" -lt 3 ]]; then
        echo "cont"
        echo "nope"

for i in 1 2 3 4 5
    echo ${i}

This is the output:


But I would expect


Go to Source
Author: Stripy42

script spawn via atd leaves a process stuck in wait

I have a bash script which is run via at, I run it like
echo "bash /path/to/my/" | at -M now because I want it
to run detached.

When the script is spawned, immediately I see 2 instances (ps), and
even after my script completes successfully, another instance just
wont terminate. It will get reparented to pid 1 and just keeps doing
wait ( strace shows wait4(-1...).

I am not able to figure out why or how this second instance is forked!
When I run the script without at, I don’t see the second process.
Any hints/tips to debug this ?


Go to Source
Author: vyom

I set my own path to $PATH variable in BASH terminal to have global access to some of my scripts, but I can not access them in any directory

I used ~/.bash_profile to add new path to my $PATH variable. I added export PATH=”/usr/sbin:$PATH” to my ~/.bash_profile, then saved it, and typed source ~/.bash_profile in my terminal. I echo $PATH and I see that I have this path in my $PATH variable. But I cannot access lsof utility (this command is in /usr/sbin directory). Also I have this sutuation with any directories. I wanted to add a directory, I added it, I had this path in my $PATH variable, but I could not access scripts that I have there.

Go to Source
Author: Dari V

Copy and paste a folder

I am trying to make a copy of an OpenFoam tutorial folder to my desktop through Ubuntu since I don’t want to mess up the original files. I use the command line:
cp -r $FOAM_TUTORIAL /mnt/c/Users/username/Desktop

but this error pops up
cp: missing destination file operand after ‘/c/Users/username/Desktop’

Can I get any help with this?

Go to Source
Author: user421564

Infinite loop when trying to launch a symbolic-link to a bash script

I am trying to create a symbolic-link by using:

$ ln -s path/to/ ~/.local/bin/foo

To the following bashscript:


  1 #!/usr/bin/env bash
  2  appname=`basename $0 | sed s,.sh$,,`
  4  dirname=`dirname $0`
  5  tmp="${dirname#?}"
  7  if [ "${dirname%$tmp}" != "/" ]; then
  8  dirname=$PWD/$dirname
  9  fi
 10  LD_LIBRARY_PATH=$dirname
 11  export LD_LIBRARY_PATH
 12  $dirname/$appname $*

I dont understand why i get the following error, when I try to launch, script above, from the symbolic link:

bash: warning: shell level (1000) too high, resetting to 1

Go to Source
Author: z3r0p1r

Bash Shell “onecmd” Option Usage Purpose

I read on man page, but I don’t understand the purpose of bash’s onecmd option. If I use the set -o onecmd command, the shell immediately exits. My expectation is for it to wait for me to enter one more command before exiting, but this does not happen.

So is this option just like the exit command?

Why does bash provide this option?

Can someone give me an example of how to properly use this option?

Go to Source
Author: testter

How do I rename files multiple files by removing everything but numbers?

I want to rename files like:

SL Benfica vs. SC Beira-Mar 136.mp4
SL Benfica vs. SC Beira-Mar 137.mp4
SL Benfica vs. SC Beira-Mar 138.mp4
SL Benfica vs. SC Beira-Mar Jogo 074.mp4
SL Benfica vs. SC Beira-Mar Jogo 082.mp4
SL Benfica vs. SC Beira-Mar Jogo 112.mp4

But this

for f in *.mp4; do echo mv "$f" "${f//[^0-9]/}.mp4"; done

Adds a “4” at the end:


I think that it gets confused with the “4” in “mp4”. How can I solve this?

Go to Source
Author: mykahveli

List & rename files that start with –

I have a 100+ files that start with a dash (-). I need to know how to do two things:

Example: -20200622_142237.jpg (File extensions vary, but all have the dash at the beginning.)

  1. How do I list (ls) files that start with this dash? Bash seems to think I’m trying to use another parameter. I’ve tried using single quotes, double quotes and – in front. Nothing seems to work.

  2. How do I rename all of these files at the same time? The intent is to rename them without the dash at the beginning.

NOTE: I don’t want to change the underscore (_). I just want to remove the dash (-) at the beginning of the file.

Lastly, I’ve already tried this possible solution posted previously, but it does not work.

Go to Source
Author: mc3mcintyre

SSH Time out Error

I am looking for new ideas on how can I do more better.

Home laptop (home Laptop can access only Linux VM)
Linux VM (This machine can access only jump box)
jump box VM

I can ssh fine from Linux VM to jump box keys are setup.

one user is set up in Linux VM called joe and Joe .bash_profile looks like this.

ssh || ssh

Note: There is two nic on jump box if one is down we can use the other nic for login.

Let’s say first nic is down and when I do telnet from my home laptop (telnet Linux VM) and provide user name joe it should automatically connect us to but one is down it’s giving message timeout and taking a long time to connect to other nic.

is there any way when I do telnet and give user name check first ssh connection if down automatically connects to other second one like in 2-3 secs?

Right now it will give us a message

Time out
time out
time out
And then it will try to connect the second nic.

I can more clarify if you guys have any more questions for me.

Go to Source
Author: John


Resolving hosts can add up to the connection attempt, so specifying an IP address directly can shave off some of those precious milliseconds. If it’s only in a local network with the IP addresses all laid out that should not be the case. At least that’s what I think.

Perhaps explicitly setting the ConnectTimeout option to a shorter one. Like so,

ssh -o ConnectTimeout=10 user@host

This can help. Adjust the number to a sweet spot that works for your ssh setup as necessary.

Catch signal when a command fails

I’m trying to build a shell that gives you suggestions if you have a typing error, but I’m having trouble to catch the error.

The user will type something and after parsing it, I will use the execvp() function. The problem I’m having is that if the command is wrong I need to catch that error. I was thinking to use a signal handler, but first I need to know what signal does the bash send when a process uses a command incorrectly.

Go to Source
Author: alvarogv