0

I have this function named st that let's me change the name of the terminal so that I understand what the terminal session is doing.

function st() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

So when I run st yarn transportation start the title of my terminal changes to yarn transportation start. So far so good.

I don't know much about bash. But it pains me that I have to now run yarn transportation start to actually run that command. Is there any way that I can run anything after st after the title is changed?

I want to run a single command that changes the title of the terminal and runs the command that I gave in the title

Piyush Chauhan
  • 797
  • 1
  • 7
  • 20

1 Answers1

0

After setting title (setting PS1), you need to make sure that right TITLE is being used so that proper command can be run.

As pointed in How to execute a bash command stored as a string with quotes and asterisk there is eval function that runs the command from a string.

$ type st 
type st
st is a function
st () 
{ 
    if [[ -z "$ORIG" ]]; then
        ORIG=$PS1;
    fi;
    TITLE="\[\e]2;$*\a\]";
    PS1=${ORIG}${TITLE};
    TITLE="$*"; # Take all the arguments
    eval $TITLE
}

You can even at a -t flag to provide a separate title and command like below

function st() {
  TITLE=$*
  COMMAND=$*
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  if [ "$1" == "-t" ]; then
    TITLE=$2;
    COMMAND=${@:3}
  fi
  TITLE="\[\e]2;$TITLE\a\]"
  PS1=${ORIG}${TITLE}
  eval $COMMAND 
}
Piyush Chauhan
  • 797
  • 1
  • 7
  • 20