Get Strings Within Commandline

I’ve got two methods to get strings or substrings within current Linux shell command-line. For instance, sometimes we want to get the file full path when source it in shell. Here are my ways to do so.

  1. Use command of lsof
    1
    2
    3
    set sourced_file = `/usr/sbin/lsof -p && -w | grep -oE "\s+\/.*\Sourced_Filename"`
    set relative_path = `dirname $sourced_file`
    set absolute_path = `chdir $relative_path && pwd`
  2. Use command of history
    1
    2
    3
    set sourced_file = `history 1 | sed -e 's/^.*source\ //g'`
    set relative_path = `dirname $sourced_file`
    set absolute_path = `chdir $relative_path && pwd`