When I run this command on Ubuntu 12.10 I get the following:
$ dir=$(gsettings get org.gnome.desktop.background picture-uri)
$ echo $dir
'file:///usr/share/backgrounds/warty-final-ubuntu.png'
I would just cleanup the value getting stored in $dir
like so:
$ dir="${dir/file:\/\//}"
$ echo $dir
'/usr/share/backgrounds/warty-final-ubuntu.png'
This will truncate the file://
from the beginning of the string. You can change this if you're getting something different. In your case:
$ dir="${dir/\/\//}"
Details
The above is using a pattern substitution, ${var/pattern/}
which will remove pattern
from the variable $var
.
Alternatives
@jthill also had a good suggestion of using Bash's "remove matching pattern prefix` notation instead. It's a little trickier to understand, IMO, but works equally as well.
Example
$ dir="\'${dir#\'file://}"
The above is removing the prefix, \'file://
from $dir
. It's replacing it with a tick, '
, followed by the remainder of $dir
without the 'file://
.
Bash man page
If you want to read up more on these features of Bash, I'd encourage you to do so. These are features that we're using above.
excerpts from Bash man page
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a
pattern just as in pathname expansion. If the pattern matches the
beginning of the value of parameter, then the result of the
expansion is the expanded value of parameter with the shortest
matching pattern (the ``#'' case) or the longest matching pattern
(the ``##'' case) deleted. If parameter is @ or *, the pattern
removal operation is applied to each positional parameter in turn,
and the expansion is the resultant list. If parameter is an array
variable subscripted with @ or *, the pattern removal operation is
applied to each member of the array in turn, and the expansion is the
resultant list.
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern
just as in pathname expansion. Parameter is expanded and the longest
match of pattern against its value is replaced with string. If
pattern begins with /, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern
begins with #, it must match at the beginning of the expanded value
of parameter. If pattern begins with %, it must match at the end of
the expanded value of parameter. If string is null, matches of
pattern are deleted and the / following pattern may be omitted.
If parameter is @ or *, the substitution operation is applied to each
positional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or *, the
substitution operation is applied to each member of the array in
turn, and the expansion is the resultant list.
Follow-up Question #1
The OP asked the following in comments below.
now i am having the following problem.. unable to open image '/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_100_board_1/wallpaper-2249773.jpg''
The issue, if you'll notice, is that there are 2 tick marks at the end of the string. I have no idea why those are there, but if you'd like to get rid of the trailing tick marks you can use this sed
command right after the previous substitution I gave you. I couldn't figure out a way to deal with the 2 single tick marks at the end just using Bash's substitution features.
dir=$(echo "$dir" | sed "s/''/'/")
Example
$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_100_board_1/wallpaper-2249773.jpg''
$ dir=$(echo "$dir" | sed "s/''/'/")
$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_100_board_1/wallpaper-2249773.jpg'
///path/to/file
? I see'file:///path/to/file'
— an URI with single quotes around it.///path/to/file
would work as a file name (the extra slashes at the beginning don't matter). – Gilles 'SO- stop being evil' Nov 16 '13 at 22:34