3

I am trying to create a snapshot using Bash script as below but error occurred.

Environment: FreeBSD, Version: 13.2, Script: Bash

#!/usr/local/bin/bash
name="17102023_1234"
zfs snapshot data/library@$name

run this simple bash script but received error as shown below.

' in named character ' 'data/library@17102023_1234

It works when I put '17102023_1234' after @ instead of variable.

Any help is appreciated. Thanks.

2
  • 2
    Check your script for dos style line breaks Commented Oct 17, 2023 at 5:38
  • Hi Gerald. I don't understand what you mean. Commented Oct 18, 2023 at 2:21

1 Answer 1

3
  • Gerald Schneider pointed out one "invisible" mistake. Some other operatings systems use two bytes, carriage return (CR) and line feed, to indicate the end of a line. POSIX™‑compliant systems like FreeBSD (mostly is) use, however, only one line feed byte, the CR thus becomes part of the line’s payload.
    name="17102023_1234"␍␊
    
    Gerald’s hypothesis is that the value of name becomes 17102023_1234␍ (the byte is invisible), so
    zfs snapshot data/library@$name
    
    calls the command
    zfs snapshot data/library@17102023_1234␍
    
    Typically this problem originates from copy‑pasting. You can use dos2unix of the converters/unix2dos port or simply annihilate the CR bytes with
    sed -e's/\r//' -i'' ./my_sript.sh # substitute CR with ε in‑place
    
  • FYI, the time and date are already part of every snapshot: inspect the creation property. I use a plain counter as snapshot name because from the creation property alone you cannot learn whether there existed any intermediate snapshots.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .