1

I have an environment variable which contains:

    C:\\Users\\u49100\\OneDrive\\Documents\\CA20-4021_BUILD_1.0.876\\Name\\sc_app

I want to modify the environment variable in a script stripping:

    C:\\Users\\u49100\\

So the result can be assigned to a new variable:

    OneDrive\\Documents\\CA20-4021_BUILD_1.0.876\\Name\\sc_app

Is there a way to do something like the following:

   NEW_VARIABLE=$FULL_DIR - $DIR_PREFIX

Where $FULL_DIR would contain the full directory and $DIR_PREFIX would contain the prefix at the start of the directory to remove.

SPlatten
  • 5,334
  • 11
  • 57
  • 128

1 Answers1

1

After a lot of searching and some playing around I've found the following:

To get the length in characters of an environment variable:

    echo ${#VARNAME}

To get a sub-string from an environment variable:

    echo ${VARNAME:start:length}

Where "start" is base 0 and "length" is the number of characters to extract, length is optional and if not supplied then just use:

    echo ${VARNAME:start}

Will result in the entire content from the specified start offset.

So for my case the solution is:

    export NEW_VARIABLE=${FULL_DIR:${#DIR_PREFIX} + 1}

To search and replace any spaces with escaped spaces:

    export NEW_VARIABLE=${NEW_VARIABLE// /\\ }

The "//" is to replace all instances if just "/" then only the first is replaced, the "/\ " is the escaped version of the space character.

SPlatten
  • 5,334
  • 11
  • 57
  • 128