Elektrine lite

← Feed

@akunohana@piefed.blahaj.zone

2026-09-19 10:40 UTC

How do you remove any trailing characters from a variable in bash? I am digitizing my dad’s video and audio tapes, for which I - after painstakingly scouring the documentation of ffmpeg, as one should - finally have found the encoding options that I’m happy with. :D But instead of retyping the PhD dissertation that are the ffmpeg commands that I use a bazillion times, I have simply started to do command substitution with for instance $(cat command_ffmpeg_COPYCODEC) or $(cat command_ffmpeg_H264) and the likes. This, however, still requires me to vim the correct filenames into these concatenated commands every time I work on a new project, so I thought, “Why not just set a variable, like I use to do with visudo as in EDITOR=/bin/vim visudo So, wanting to do a little ffmpeg magic to delay the audio stream of a file, I tried FILENAME='birthday_0' $(cat command_ffmpeg_SYNCAUD) but bash said, “no, who do you think you are?” After doing a wc on FILENAME, I noticed that there is an invisible trailing something after the filename - possibly some whitespace char - since it thinks that I mean to use an option “-synced.mkv”. How would you go about writing the now bash script (it simply wouldn’t expand the variable through command substitution…) so that the trailing whatever is deleted, if that is indeed the case? I have tried slapping " “, ' ‘, ( ), { } in all conceivable configurations, but I’m simply to inexperienced. xD Please advise! 🛠️

Replies (6)

  • @p_consti@lemmy.world 2026-09-19 10:51

    Why are you not simply writing a script with an argument? Why this cat indirection with environment variables?

    Open ##4761117

  • @thingsiplay@lemmy.ml 2026-09-19 10:57

    First, did you look if the filename itself contains a trailing whitespace? I had this issue a few times myself, thinking the script is broken, but in fact the file on the filesystem had a space. That is annoying. If your variable has an additional space and you want to remove it, then I think its better to find out where this space comes from (if its not the file itself), and solve the issue there. Because if you get an unexpected space, then this could be a bug in your script somewhere. There are multiple ways to remove leading and trailing whitespaces with Bash. The best is not to call a command for this and rely on Bash substitution. This can get ugly, but luckily we can steal from Stack Overflow, :p (basically what we did before Ai): trim() { local var="$*" # remove leading whitespace characters var="${var#"${var%%[![:space:]]*}"}" # remove trailing whitespace characters var="${var%"${var##*[![:space:]]}"}" printf '%s' "$var" } stackoverflow.com/…/how-can-i-trim-whitespace-fro… (who got it from another source,

    Open ##4761184

  • @merde@sh.itjust.works 2026-09-19 11:02

    can’t you tr "endofthefilename " “endofthefilename”

    Open ##4761279

  • @talkingpumpkin@lemmy.world 2026-09-19 11:02

    FILENAME='birthday_0' $(cat command_ffmpeg_SYNCAUD) cat prints out a file without altering it’s contents. If it’s important to keep the commands in a separate file, you could use sed to find/replace, or use eval, or use some external template engine (lookup “bash expand variables in file”). What you probably want to do instead, I assume, is to replace the external files with regular bash variables: filename="some-file" command="ffmpg -i ${filename}.mpg -bla -blabla ${filename}.mkv" echo "$command"

    Open ##4761280

  • @hexagonwin@lemmy.today 2026-09-19 18:25

    i’m also a noob, but why not just do something like #!/bin/sh ffmpeg (options) $1 and run the script as ./script.sh filename.mkv?

    Open ##4766727

  • @MonkderVierte@lemmy.zip 2026-09-23 09:25

    I tend to do config loaders around commands like ffmpeg, magick, tidy. Just too much to research the switches each time you need them.

    Open ##4816424