I have this script full of a bashrc functions which is currently working well,
But there is a particular function I'm working on, inserting a line of text in a text file from user input using a read
line. The insert function works as expected, except if the input is a carriage return in other words ONLY the [enter] key.
My current insert lines are:
Code: Select all
read -p 'Type line number to insert: ' LINENUM
read -p 'Enter text to insert: ' INSERTTEXT
sed -i "${LINENUM}i ${INSERTTEXT}" ${TASKS_DIR}"${*}"
${TASKS_DIR}"${*}"
being the file to modify in place. And as I say, it works UNLESS only the [enter] key is the input from the read -p INSERTTEXT
which then returns the following error:
sed: -e expression #1, char 3: expected \ after `a', `c' or `i'
Using the expected backslash does result in a successful new line insert from the [enter] key input, BUT the backslash adds a space to all other input.
Is there a way to write the sed expression to either undo the space added to normal character input, or a better way to handle the carriage return?
Or is there a better command to use in this instance?