Storing the first 4 characters of a filename into a variable

bash

Solution

From http://tldp.org/LDP/abs/html/string-manipulation.html

Substring Extraction

`${string:position}` Extracts substring from $string at $position.

If the $string parameter is "*" or "@", then this extracts the positional parameters, [1] starting at $position.

`${string:position:length}` Extracts $length characters of substring from $string at $position.

Example

shortName=${f:0:4}

Have fun!

Problem

This follwing code is part of a script I am writing. Now, for the purposes of this script, I am assuming there is only 1 file in ./src, so this loop should only execute once. Now, in the loop somewhere, I want to take the first 4 characters of $f (the filename) and store it in another variable. I know there is the cut command but I'm not sure if or how that would be used here because I thought cut was used for contents of files, not the files themselves. ``` for f in `ls ./src` do echo $f cd tmp f="../src/$f" sh "$f" done ```

Original source