Change blank spaces with underscores in Bash -



Change blank spaces with underscores in Bash -

i'd write script in bash that:

takes file input.

if file name has blank space changes underscore.

my first question is: can bash script take arguments input or input has provided 1 time script running means of read command? other question how utilize mv command perform point 2.

your script simply:

#!/bin/bash mv "$1" "$(sed 's/ \{1,\}/_/g' <<<"$1")"

use script.sh file\ name. file file name renamed file_name.

as gniourf_gniourf has pointed out in comments (thanks), if using bash possible utilize built-in character substitution:

mv "$1" "${1// /_}"

the behaviour of different, replace every individual space underscore, whereas approach using sed squashes groups of spaces single underscore. if enable extended globbing, can create version behave same 1 using sed:

shopt -s extglob mv "$1" "${1//+( )/_}"

depending on whether looking replace space characters or kinds of white space (like tabs, newlines, etc.), may want utilize [[:space:]] rather in each of these examples.

bash

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -